w3school的PHP教程提炼(二)PHP高级
1 Date()
1.1 Date()函数
语法:date(format,timestamp)
format:必需。规定时间戳的格式。
timestamp:可选。规定时间戳。默认是当前的日期和时间。
1.2 时间戳(Timestamp)
自1970.1.1(00:00:00GMT)以来的秒数
1.3 格式化日期
date()函数的format参数规定了如何格式化日期/时间
d:月中的天(01-31)
m:当前的月,以数字计(01-12)
Y:当前的年(四位数)
<? php
echo date ( " Y/m/d " );
echo " <br /> " ;
echo date ( " Y-m-d " );
?>
1.4 添加时间戳
date()函数的timestamp参数规定了一个时间戳
1.5 mktime()函数
mktime()函数可为指定的日期返回Unix时间戳
语法:mktime(hour,minute,second,month,day,year,is_dst)
<? php
$tomorrow = mktime ( 0 , 0 , 0 , date ( " m " ) , date ( " d " ) + 1 , date ( " Y " ));
echo " Tomorrow is " . date ( " Y/m/d " , $tomorrow );
?>
2 引用文件
2.1 include()函数
<? php include ( " header.php " ); ?>
2.2 require()函数
和include()的错误处理方式不同:include()函数会生成一个警告(但是脚本会继续执行),而require()函数会生成一个致命错误(fatal error)(在错误发生后脚本会停止执行)
3 文件处理
3.1 打开文件 fopen()函数
<? php
$file = fopen ( " welcome.txt " , " r " );
?>
第一个参数含有要打开的文件的名称
第二个参数规定了使用哪种模式来打开文件
模式 |
描述 |
r |
只读。在文件的开头开始。 |
r+ |
读/写。在文件的开头开始。 |
w |
只写。打开并清空文件的内容;如果文件不存在则创建新文件 |
w+ |
读/写。打开并清空文件的内容;如果文件不存在则创建新文件 |
a |
追加。打开并向文件末端进行写操作,如果文件不存在则创建新文件 |
a+ |
读/追加。通过向文件末端写内容,来保持文件内容 |
x |
只写。创建新文件。如果文件已存在则返回false |
x+ |
读/写。创建新文件。如果文件已存在,则返回false和一个错误。如果fopen()无法打开指定文件,则返回0(false) |
3.2 关闭文件 fclose()函数
<? php
$file = fopen ( " test.txt " , " r " );
// ...
fclose ( $file );
?>
3.3 检测End-of-file feof()函数
<? php
if ( feof ( $file )){
echo " End of file " ;
}
?>
3.4 逐行读取文件 fgets()函数
在调用该函数之后,文件指针会移动到下一行
<? php
$file = fopen ( " welcome.txt " , " r " ) or exit ( " Unable to open file! " );
while ( ! feof ( $file )){
echo fgets ( $file ) . " <br /> " ;
}
fclose ( $file );
?>
3.5 逐字符读取文件 fgetc()函数
在调用该函数之后,文件指针会移动到下一个字符
<? php
$file = fopen ( " welcome.txt " , " r " ) or exit ( " Unable to open file! " );
while ( ! feof ( $file )){
echo fgetc ( $file );
}
fclose ( $file );
?>
4 文件上传
4.1 创建表单
<? php
$file = fopen ( " welcome.txt " , " r " ) or exit ( " Unable to open file! " );
while ( ! feof ( $file )){
echo fgetc ( $file );
}
fclose ( $file );
?>
4.2 上传脚本 全局数组$_FILES
<? php
if ( $_FILES [ " file " ][ " error " ] > 0 ){
echo " Error: " . $_FILES [ " file " ][ " error " ] . " <br /> " ;
} else {
echo " Upload: " . $_FILES [ " file " ][ " name " ] . " <br /> " ;
echo " Type: " . $_FILES [ " file " ][ " type " ] . " <br /> " ;
echo " Size: " . ( $_FILES [ " file " ][ " size " ] / 1024 ) . " Kb<br /> " ;
echo " Stored in: " . $_FILES [ " file " ][ " tmp_name " ];
}
?>
$_FILES数组的第一个参数是表单的input name,第二个下标可以是:
name:被上传文件的名称
type:被上传文件的类型
size:被上传文件的大小
tmp_name:存储在服务器的文件的临时副本的名称
error:由文件上传导致的错误代码
4.3 上传限制
限制上传.gif或者.jpeg文件,文件大小必须小于20kb:
<? php
if ((( $_FILES [ " file " ][ " type " ] == " images/gif "|| $_FILES [ " file " ][ " type " ] == " images/jpeg "|| $_FILES [ " file " ][ " type " ] == " images/pjpeg " ))&& ( $_FILES [ " file " ][ " size " ] < 20000 )){ // IE识别jpg为pjpeg,FF识别jpg为jpeg
if ( $_FILES [ " file " ][ " error " ] > 0 ){
echo " Error: " . $_FILES [ " file " ][ " error " ] . " <br /> " ;
} else {
echo " Upload: " . $_FILES [ " file " ][ " name " ] . " <br /> " ;
echo " Type: " . $_FILES [ " file " ][ " type " ] . " <br /> " ;
echo " Size: " . ( $_FILES [ " file " ][ " size " ] / 1024 ) . " Kb<br /> " ;
echo " Stored in: " . $_FILES [ " file " ][ " tmp_name " ];
} else {
echo " Invalid file " ;
}
}
?>
4.4 保存被上传的文件
将上面例子在临时文件夹创建的被上传文件的临时副本拷贝到另外的位置以免其在脚本结束时消失
<? php
if ((( $_FILES [ " file " ][ " type " ] == " images/gif "|| $_FILES [ " file " ][ " type " ] == " images/jpeg "|| $_FILES [ " file " ][ " type " ] == " images/pjpeg " ))&& ( $_FILES [ " file " ][ " size " ] < 20000 )){ // IE识别jpg为pjpeg,FF识别jpg为jpeg
if ( $_FILES [ " file " ][ " error " ] > 0 ){
echo " Error: " . $_FILES [ " file " ][ " error " ] . " <br /> " ;
} else {
echo " Upload: " . $_FILES [ " file " ][ " name " ] . " <br /> " ;
echo " Type: " . $_FILES [ " file " ][ " type " ] . " <br /> " ;
echo " Size: " . ( $_FILES [ " file " ][ " size " ] / 1024 ) . " Kb<br /> " ;
echo " Temp file: " . $_FILES [ " file " ][ " tmp_name " ] . " <br /> " ;
if ( file_exists ( " upload/ " . $_FILES [ " file " ][ " name " ])){
echo $_FILES [ " file " ][ " name " ] . " already exists. " ;
} else {
move_uploaded_file ( $_FILES [ " file " ][ " tmp_name " ] , " upload/ " . $_FILES [ " file " ][ " name " ]);
echo " Stored in: " . " upload/ " . $_FILES [ " file " ][ " name " ];
}
}
} else {
echo " Invalid file " ;
}
?>
5 Cookies
5.1 创建cookie setcookie()函数
语法:setcookie(name, value, expire, path, domain);
<? php
setcookie ( " user " , " Alex Porter " , time () + 3600 );// 创建名为"user"的cookie,赋值为Alex Porter,1小时后过期
?>
5.2 取回cookie的值
<? php
echo $_COOKIE [ " user " ]; // print a cookie
print_r ( $_COOKIE ); // a way to view all cookies
?>
使用isset()函数来确认是否已设置了cookie:
<? php
if ( isset ( $_COOKIE [ " user " ])){
echo " Welcome " . $_COOKIE [ " user " ] . " !<br /> " ;
} else {
echo " Welcome guest!<br /> " ;
}
?>
5.3 删除cookie
<? php
setcookie ( " user " , "" , time () - 3600 ); // 使过期日期变更为过去的时间点
?>
6 Session变量
6.1 启动session会话
<? php session_start (); ?><!-- 该函数必须位于html标签之前 -->
< html >
</ html >
6.2 存储session变量
<? php
session_start ();
$_SESSION [ " views " ] = 1 ; // store session data
?>
< html >
< body >
<? php
echo " Pageviews= " . $_SESSION [ ' views ' ]; // print Pageviews=1
?>
</ body >
</ html >
6.3 终结session
<? php
unset ( $_SESSION [ ' views ' ]); // 释放指定的session变量
?>
<? php
session_destroy (); // 彻底终结session
?>
7 mail()函数 用于从脚本中发送电子邮件。
语法:mail(to, subject, message, headers, parameters)
参数 |
描述 |
to |
必需。规定email接收者。 |
subject |
必需。规定email的主题。注释:该参数不能包含任何新行字符。 |
message |
必需。定义要发送的信息。应使用LF(\n)来分隔各行。 |
headers |
可选。规定附加的标题,比如From、Cc以及Bcc。应当使用过CRLF(\r\n)分隔附加的标题。 |
parameters |
可选。对邮件发送程序规定额外的参数。 |
7.1 范例
<? php
$to = " [email protected] " ;
$subject = " Test mail " ;
$message = " Hello!This is a simple email message. " ;
$from = " [email protected] " ;
$headers = " From: $from " ;
mail ( $to , $subject , $message , $headers );
echo " Mail Sent. " ;
?>