#################################
##########BY:Moskey.Tong#########
###########2009-11-15############
#########*转载请注明*############
#################################
总结一下这个星期天PHP上传图片。
上传图片原理:首先判断文件类型是否为图片格式,若是则上传文件,然后重命名文件(一般都是避免上传文件重名,现在基本上都是以为时间来命名),接着把文件上传到指定目录,成功上传后输出上传图片的预览。
1.首先我们开始判断文件类型是否为图片类型
用到的函数
{
strrchr:查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。
substr: 取部份字符串。
$HTTP_POST_FILES['file']['name']:获取当前上传的文件全称。
}
图片类型就是“.”后面的字符(比如:一个文件名称为XXX.JPG 那么它的类型就是“.”后面的JPG)。
我们可以用PHP中的函数来截取上传者文件名字的。我们来写个获取文件类型的函数
<?
function type()
{
return substr(strrchr($HTTP_POST_FILES['file']['name'],'.'),1);
}
?>
2.若是则上传文件,然后重命名文件
用到的函数
{
st ......
Php Variable Circular References
Circular-references has been a long outstanding issue with PHP. They are
caused by the fact that PHP uses a reference counted memory allocation
mechanism for its internal variables. This causes problems for longer
running scripts (such as an Application Server
or
the eZ Components
test-suite) as the memory is not freed until the end of the request. But
not everybody is aware on what exactly the problem is, so here is a
small introduction to circular references in PHP.
In PHP a refcount value is kept for every variable container (zval).
Those containers are pointed to from a function's symbol table that
contains the names of all the variables in the function's scope. Every
variable, array element or object property that points to a zval will
increase its refcount by one. The refcount of a zval container is
decreased by one whenever call unset() on a variable name that points to
it, or when a variable goes away because the ......
在PHP里,如果你没有手写构造函数,则php在实例化这个对象的时候,会自动为类成员以及类方法进行初始化,分配内存等工作,但是有些时候不能满足我们的要求,比如我们要在对象实例化的时候传递参数,那么就需要手动编写构造函数了,手写构造函数有两种写法,只是表现形式不同,其实本质一样
class test
{
function __construct()
{
//your code
}
}
class test
{
function test()//如果方法名跟类名字一样,将被认为是构造函数
{
//your code
}
}
以上为两种基本形式
传递参数进行实例化的例子,简单的写一个参考
class test
{
public $test = '';
function __construct($input = '')
{
$this->test = $input;
}
function getTest()
{
return $this->test;
......
整理活:PHP的日期时间函数date()
1,年-月-日
echo date('Y-m-j');
2007-02-6
echo date('y-n-j');
07-2-6
大写Y表示年四位数字,而小写y表示年的两位数字;
小写m表示月份的数字(带前导),而小写n则表示不带前导的月份数字。
echo date('Y-M-j');
2007-Feb-6
echo date('Y-m-d');
2007-02-06
大写M表示月份的3个缩写字符,而小写m则表示月份的数字(带前导0);
没有大写的J,只有小写j表示月份的日期,无前导o;若需要月份带前导则使用小写d。
echo date('Y-M-j');
2007-Feb-6
echo date('Y-F-jS');
2007-February-6th
大写M表示月份的3个缩写字符,而大写F表示月份的英文全写。(没有小写f)
大写S表示日期的后缀,比如“st”、“nd”、“rd”和“th”,具体看日期数字为何。
小结:
表示年可以用大写的Y和小写y;
表示月可以用大写F、大写M、小写m和小写n(分别表示字符和数字的两种方式);
表示日可以用小写d和小写j,大写S表示日期的后缀。
2,时:分:秒
默认情况下,PHP解释显示的时间为“格林威治标准时间”,与我们本地的时间相差8个小时。
echo date('g:i:s a');
5:56:57 am
echo date('h:i ......
摘要: 用正则实现包含某个字符串很容易,但如果实现不包含某个字符串呢?作者给出了一个解决方案。
判断一个字符串中是否含有另一字符串,php有很多方法,如下:
1. 常见函数
strstr($str, "abc");
strstr($str, "abc");
2. 正则匹配
preg_match("/(abc)/is", $str);
preg_match("/(abc)/is", $str);
但是要匹配一个字符串中,不包含某字符串,用正则就比较麻烦了。
1. 如果不用正则如下就可以解决问题
!strstr($str, "abc");
!strstr($str, "abc");
2. 但是用正则呢,就只有这样了
preg_match("/^((?!abc).)*$/is", $str);
preg_match("/^((?!abc).)*$/is", $str);
完整代码示例
$str = "dfadfadf765577abc55fd";
$pattern_url = "/^((?!abc).)*$/is";
if (preg_match($pattern_url, $str))
{
echo "不含有abc!";
}
else
{
echo "含有abc!"; ......
PHP中类的方法也和C++通过关键字 private
、Public
、 protected
来控制的。下边是详细说明:
1、private
函数前使用关键字Private表示函数是私有的,私有的意思顾名思义,针对当前类私有,无论是他的子类还是他的实例都无法访问。
还是先看代码:
<?php
class Persion
{
function Persion()
{
echo "Persion OK";
}
private function test1()
{
echo "test1.......";
}
protected function test2()
{
echo "test2.....";
}
public function test3()
{
echo "test3....";
}
}
class Child extends Persion
{
function Child()
{
echo "Child OK".'<br>';
//调用父类的私有方法
Persion::test1();
}
}
$c = new Child;
//实例中调用私有方法
$c->test1();
?>
以上代码注释部分的调用都会引起报错,所以Private标记的函数只能在当前类中使用。
2、protected
......