<?php
class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;
function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=”";
}
<?php
class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;
function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=”";
}
在 PHP 应用中,正则表达式主要用于:
•正则匹配:根据正则表达式匹配相应的内容
•正则替换:根据正则表达式匹配内容并替换
•正则分割:根据正则表达式分割字符串
PHP的显示时间的代码比ASP还是强大多了,调用起来更加的简单。
先看
上传操作代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
</head>
<body>
<?php
if($_FILES['myfile']['error'] > 0) { //判断文件是否可以成功上传到服务器,0表示上传成功
echo 'Error: ';
switch ( $_FILES['myfile']['error'] ) {
case UPLOAD_ERR_OK:
$response = 'There is no error, the file uploaded with success.';
break;
case UPLOAD_ERR_INI_SIZE:
$response = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
break;
case UPLOAD_ERR_FORM_SIZE:
$response = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
break;
case UPLOAD_ERR_PARTIAL:
$response = 'The uploaded file was only partially uploaded.';
break;
case UPLOAD_ERR_NO_FILE:
$response = 'No file was uploaded.';
break;
case UPLOAD_ERR_NO_TMP_DIR:
$response = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
break;
case UPLOAD_ERR_CANT_WRITE:
$response = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
break;
case UPLOAD_ERR_EXTENSION:
$response = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
break;
default:
$response = 'Unknown error';
break;
}
echo $response;
exit; //如果$_FILES['myfile']['error']大于0都是有错误,输出错误信息并退出程序
}
//获取上传文件的MIME类型中的主类型和子类型
list($maintype,$subtype)=explode("/",$_FILES['myfile']['type']);
if ($maintype=="text") { //通过主类型限制不能上传文本文件,例如.txt .html .php等文件文件
echo '问题: 不能上传文本文件。';
exit; //如果用户上传文本文件则退出程序
}
$upfile = './uploads/'.time().$_FILES['myfile']['name']; //定义上传后的位置和新文件名
if ( is_uploaded_file($_FILES['myfile']['tmp_name']) ) { //判断是否为上传文件
if ( !move_uploaded_file($_FILES['myfile']['tmp_name'], $upfile) ) { //从移动文件
echo '问题: 不能将文件移动到指定目录。';
exit;
}
}else{
echo '问题: 上传文件不是一个合法文件: ';
echo $_FILES['myfile']['name'];
exit;
}
echo '文件 : '.$upfile.' 上传成功, 大小为 : ' .$_FILES['myfile']['size'].'!<br>'; //如果文件上传成功则输出
?>
</body>
</html>
function RecordToJson($recordset)
{
$jstr='[';
while($rs = $recordset->Fetch())
{
//$nick = iconv("GBK",'utf-8',$rs['nick']);/*转换为utf-8编码*/
//TODO:遍历结果集
$arr_keys=array_keys($rs);
$jstr=$jstr.'{';
for($i=0;$i<count($arr_keys);$i+=2)
{
//数据库编码为gbk,需要转换编码
//TODO;iconv("GBK",'utf-8',$rs['nick']);/*转换为utf-8编码*/
$key=iconv("GBK",'utf-8',$arr_keys[$i]);//$arr_keys[$i];
$value=iconv("GBK",'utf-8',$rs[$arr_keys[$i]]);//$rs[$arr_keys[$i]];
$jstr=$jstr.'"'.$key.'":"'.$value.'",';
}
$jstr=substr($jstr,0,strlen($jstr)-1);
$jstr=$jstr.'},';
}
$jstr=substr($jstr,0,strlen($jstr)-1);
$jstr=$jstr.']';
return $jstr;
}
is_file 只判断文件是否存在;
<?php
$file = "test.txt";
if(is_file($file)) {
echo ("$file is a regular file");
}else {
echo ("$file is not a regular file");
}
?>
取部份字符串。
语法: string substr(string string, int start, int [length]);
分享一个实际在用的函数:
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
1,年-月-日
echo date('Y-m-j');
2007-02-6
echo date('y-n-j');
07-2-6
1. 判断Email:
<?php
function is_email($email){
return strlen($email) > 6 && preg_match("/^[w-.]+@[w-]+(.w+)+$/", $email);
}
?>
代码如下:
不过这个函数有个局限,就是$start子串和$end子串在整个串中只能出现一次。请看下面的例子: