如题所述
æ´æ¹xhEditor ä¸ä¼ æ件upload.php
æä¸ä¼ å®çå¾çè·¯å¾æ¹ä¸º http è·¯å¾
<?php
/*!
* 注2ï¼æ¬ç¨åºç¹å«é对HTML5ä¸ä¼ ï¼å å ¥äºç¹æ®å¤ç
*/
header('Content-Type: text/html; charset=UTF-8');
//------å®ä¹httpè·¯å¾
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$dir_url_http=dirname($url).'/';
//-------------------------------------
$inputName='filedata';//表åæ件åname
$attachDir='upload';//ä¸ä¼ æ件ä¿åè·¯å¾ï¼ç»å°¾ä¸è¦å¸¦/
$dirType=1;//1:æ天åå ¥ç®å½ 2:ææåå ¥ç®å½ 3:ææ©å±ååç®å½ 建议使ç¨æ天å
$maxAttachSize=2097152;//æ大ä¸ä¼ 大å°ï¼é»è®¤æ¯2M
$upExt='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//ä¸ä¼ æ©å±å
$msgType=2;//è¿åä¸ä¼ åæ°çæ ¼å¼ï¼1ï¼åªè¿åurlï¼2ï¼è¿ååæ°æ°ç»
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;//ç«å³ä¸ä¼ 模å¼ï¼ä» 为æ¼ç¤ºç¨
ini_set('date.timezone','Asia/Shanghai');//æ¶åº
$err = "";
$msg = "''";
$tempPath=$attachDir.'/'.date("YmdHis").mt_rand(10000,99999).'.tmp';
$localName='';
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5ä¸ä¼
file_put_contents($tempPath,file_get_contents("php://input"));
$localName=urldecode($info[2]);
}
else{//æ å表åå¼ä¸ä¼
$upfile=@$_FILES[$inputName];
if(!isset($upfile))$err='æ件åçnameé误';
elseif(!empty($upfile['error'])){
switch($upfile['error'])
{
case '1':
$err = 'æ件大å°è¶ è¿äºphp.iniå®ä¹çupload_max_filesizeå¼';
break;
case '2':
$err = 'æ件大å°è¶ è¿äºHTMLå®ä¹çMAX_FILE_SIZEå¼';
break;
case '3':
$err = 'æ件ä¸ä¼ ä¸å®å ¨';
break;
case '4':
$err = 'æ æ件ä¸ä¼ ';
break;
case '6':
$err = '缺å°ä¸´æ¶æ件夹';
break;
case '7':
$err = 'åæ件失败';
break;
case '8':
$err = 'ä¸ä¼ è¢«å ¶å®æ©å±ä¸æ';
break;
case '999':
default:
$err = 'æ ææé误代ç ';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = 'æ æ件ä¸ä¼ ';
else{
move_uploaded_file($upfile['tmp_name'],$tempPath);
$localName=$upfile['name'];
}
}
if($err==''){
$fileInfo=pathinfo($localName);
$extension=$fileInfo['extension'];
if(preg_match('/^('.str_replace(',','|',$upExt).')$/i',$extension))
{
$bytes=filesize($tempPath);
if($bytes > $maxAttachSize)$err='请ä¸è¦ä¸ä¼ 大å°è¶ è¿'.formatBytes($maxAttachSize).'çæ件';
else
{
switch($dirType)
{
case 1: $attachSubDir = 'day_'.date('ymd'); break;
case 2: $attachSubDir = 'month_'.date('ym'); break;
case 3: $attachSubDir = 'ext_'.$extension; break;
}
$attachDir = $attachDir.'/'.$attachSubDir;
if(!is_dir($attachDir))
{
@mkdir($attachDir, 0777);
@fclose(fopen($attachDir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$newFilename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$targetPath = $attachDir.'/'.$newFilename;
rename($tempPath,$targetPath);
@chmod($targetPath,0755);
$targetPath=jsonString($targetPath);
if($immediate=='1')$targetPath='!'.$targetPath;
if($msgType==1)$msg="'$targetPath'";
else $msg="{'url':'".$dir_url_http.$targetPath."','localname':'".jsonString($localName)."','id':'1'}";//idåæ°åºå®ä¸åï¼ä» ä¾æ¼ç¤ºï¼å®é 项ç®ä¸å¯ä»¥æ¯æ°æ®åºID
}
}
else $err='ä¸ä¼ æ件æ©å±åå¿ é为ï¼'.$upExt;
@unlink($tempPath);
}
echo "{'err':'".jsonString($err)."','msg':".$msg."}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
?>
æä¸ä¼ å®çå¾çè·¯å¾æ¹ä¸º http è·¯å¾
<?php
/*!
* 注2ï¼æ¬ç¨åºç¹å«é对HTML5ä¸ä¼ ï¼å å ¥äºç¹æ®å¤ç
*/
header('Content-Type: text/html; charset=UTF-8');
//------å®ä¹httpè·¯å¾
$url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
$dir_url_http=dirname($url).'/';
//-------------------------------------
$inputName='filedata';//表åæ件åname
$attachDir='upload';//ä¸ä¼ æ件ä¿åè·¯å¾ï¼ç»å°¾ä¸è¦å¸¦/
$dirType=1;//1:æ天åå ¥ç®å½ 2:ææåå ¥ç®å½ 3:ææ©å±ååç®å½ 建议使ç¨æ天å
$maxAttachSize=2097152;//æ大ä¸ä¼ 大å°ï¼é»è®¤æ¯2M
$upExt='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//ä¸ä¼ æ©å±å
$msgType=2;//è¿åä¸ä¼ åæ°çæ ¼å¼ï¼1ï¼åªè¿åurlï¼2ï¼è¿ååæ°æ°ç»
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;//ç«å³ä¸ä¼ 模å¼ï¼ä» 为æ¼ç¤ºç¨
ini_set('date.timezone','Asia/Shanghai');//æ¶åº
$err = "";
$msg = "''";
$tempPath=$attachDir.'/'.date("YmdHis").mt_rand(10000,99999).'.tmp';
$localName='';
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5ä¸ä¼
file_put_contents($tempPath,file_get_contents("php://input"));
$localName=urldecode($info[2]);
}
else{//æ å表åå¼ä¸ä¼
$upfile=@$_FILES[$inputName];
if(!isset($upfile))$err='æ件åçnameé误';
elseif(!empty($upfile['error'])){
switch($upfile['error'])
{
case '1':
$err = 'æ件大å°è¶ è¿äºphp.iniå®ä¹çupload_max_filesizeå¼';
break;
case '2':
$err = 'æ件大å°è¶ è¿äºHTMLå®ä¹çMAX_FILE_SIZEå¼';
break;
case '3':
$err = 'æ件ä¸ä¼ ä¸å®å ¨';
break;
case '4':
$err = 'æ æ件ä¸ä¼ ';
break;
case '6':
$err = '缺å°ä¸´æ¶æ件夹';
break;
case '7':
$err = 'åæ件失败';
break;
case '8':
$err = 'ä¸ä¼ è¢«å ¶å®æ©å±ä¸æ';
break;
case '999':
default:
$err = 'æ ææé误代ç ';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = 'æ æ件ä¸ä¼ ';
else{
move_uploaded_file($upfile['tmp_name'],$tempPath);
$localName=$upfile['name'];
}
}
if($err==''){
$fileInfo=pathinfo($localName);
$extension=$fileInfo['extension'];
if(preg_match('/^('.str_replace(',','|',$upExt).')$/i',$extension))
{
$bytes=filesize($tempPath);
if($bytes > $maxAttachSize)$err='请ä¸è¦ä¸ä¼ 大å°è¶ è¿'.formatBytes($maxAttachSize).'çæ件';
else
{
switch($dirType)
{
case 1: $attachSubDir = 'day_'.date('ymd'); break;
case 2: $attachSubDir = 'month_'.date('ym'); break;
case 3: $attachSubDir = 'ext_'.$extension; break;
}
$attachDir = $attachDir.'/'.$attachSubDir;
if(!is_dir($attachDir))
{
@mkdir($attachDir, 0777);
@fclose(fopen($attachDir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$newFilename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$targetPath = $attachDir.'/'.$newFilename;
rename($tempPath,$targetPath);
@chmod($targetPath,0755);
$targetPath=jsonString($targetPath);
if($immediate=='1')$targetPath='!'.$targetPath;
if($msgType==1)$msg="'$targetPath'";
else $msg="{'url':'".$dir_url_http.$targetPath."','localname':'".jsonString($localName)."','id':'1'}";//idåæ°åºå®ä¸åï¼ä» ä¾æ¼ç¤ºï¼å®é 项ç®ä¸å¯ä»¥æ¯æ°æ®åºID
}
}
else $err='ä¸ä¼ æ件æ©å±åå¿ é为ï¼'.$upExt;
@unlink($tempPath);
}
echo "{'err':'".jsonString($err)."','msg':".$msg."}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答 2016-03-20
upload.php文件内容,如下:
<?php
header('Content-Type: text/html; charset=UTF-8');
$inputName='filedata';//表单文件域name
$attachDir='news';//上传文件保存路径,结尾不要带/
$dirType=1;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
$maxAttachSize=2097152;//最大上传大小,默认是2M
$upExt='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//上传扩展名
$msgType=2;//返回上传参数的格式:1,只返回url,2,返回参数数组
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;
ini_set('date.timezone','Asia/Shanghai');//时区
$err = "";
$msg = "''";
$tempPath=$attachDir.'/'.date("YmdHis").mt_rand(10000,99999).'.tmp';
$localName='';
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5上传
file_put_contents($tempPath,file_get_contents("php://input"));
$localName=urldecode($info[2]);
}
else{//标准表单式上传
$upfile=@$_FILES[$inputName];
if(!isset($upfile))$err='文件域的name错误';
elseif(!empty($upfile['error'])){
switch($upfile['error'])
{
case '1':
$err = '文件大小超过了php.ini定义的upload_max_filesize值';
break;
case '2':
$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
break;
case '3':
$err = '文件上传不完全';
break;
case '4':
$err = '无文件上传';
break;
case '6':
$err = '缺少临时文件夹';
break;
case '7':
$err = '写文件失败';
break;
case '8':
$err = '上传被其它扩展中断';
break;
case '999':
default:
$err = '无有效错误代码';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
else{
move_uploaded_file($upfile['tmp_name'],$tempPath);
$localName=$upfile['name'];
}
}
if($err==''){
$fileInfo=pathinfo($localName);
$extension=$fileInfo['extension'];
if(preg_match('/^('.str_replace(',','|',$upExt).')$/i',$extension))
{
$bytes=filesize($tempPath);
if($bytes > $maxAttachSize)$err='请不要上传大小超过'.formatBytes($maxAttachSize).'的文件';
else
{
switch($dirType)
{
case 1: $attachSubDir = 'day_'.date('ymd'); break;
case 2: $attachSubDir = 'month_'.date('ym'); break;
case 3: $attachSubDir = 'ext_'.$extension; break;
}
$attachDir = $attachDir.'/'.$attachSubDir;
if(!is_dir($attachDir))
{
@mkdir($attachDir, 0777);
@fclose(fopen($attachDir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$newFilename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$targetPath = $attachDir.'/'.$newFilename;
rename($tempPath,$targetPath);
@chmod($targetPath,0755);
$targetPath=jsonString($targetPath);
if($immediate=='1')$targetPath='!'.$targetPath;
if($msgType==1)$msg="'$targetPath'";
else $msg="{'url':'".$targetPath."','localname':'".jsonString($localName)."','id':'1'}";
}
}
else $err='上传文件扩展名必需为:'.$upExt;
@unlink($tempPath);
}
echo "{'err':'".jsonString($err)."','msg':".$msg."}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
?>本回答被提问者采纳
<?php
header('Content-Type: text/html; charset=UTF-8');
$inputName='filedata';//表单文件域name
$attachDir='news';//上传文件保存路径,结尾不要带/
$dirType=1;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
$maxAttachSize=2097152;//最大上传大小,默认是2M
$upExt='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//上传扩展名
$msgType=2;//返回上传参数的格式:1,只返回url,2,返回参数数组
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;
ini_set('date.timezone','Asia/Shanghai');//时区
$err = "";
$msg = "''";
$tempPath=$attachDir.'/'.date("YmdHis").mt_rand(10000,99999).'.tmp';
$localName='';
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5上传
file_put_contents($tempPath,file_get_contents("php://input"));
$localName=urldecode($info[2]);
}
else{//标准表单式上传
$upfile=@$_FILES[$inputName];
if(!isset($upfile))$err='文件域的name错误';
elseif(!empty($upfile['error'])){
switch($upfile['error'])
{
case '1':
$err = '文件大小超过了php.ini定义的upload_max_filesize值';
break;
case '2':
$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
break;
case '3':
$err = '文件上传不完全';
break;
case '4':
$err = '无文件上传';
break;
case '6':
$err = '缺少临时文件夹';
break;
case '7':
$err = '写文件失败';
break;
case '8':
$err = '上传被其它扩展中断';
break;
case '999':
default:
$err = '无有效错误代码';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
else{
move_uploaded_file($upfile['tmp_name'],$tempPath);
$localName=$upfile['name'];
}
}
if($err==''){
$fileInfo=pathinfo($localName);
$extension=$fileInfo['extension'];
if(preg_match('/^('.str_replace(',','|',$upExt).')$/i',$extension))
{
$bytes=filesize($tempPath);
if($bytes > $maxAttachSize)$err='请不要上传大小超过'.formatBytes($maxAttachSize).'的文件';
else
{
switch($dirType)
{
case 1: $attachSubDir = 'day_'.date('ymd'); break;
case 2: $attachSubDir = 'month_'.date('ym'); break;
case 3: $attachSubDir = 'ext_'.$extension; break;
}
$attachDir = $attachDir.'/'.$attachSubDir;
if(!is_dir($attachDir))
{
@mkdir($attachDir, 0777);
@fclose(fopen($attachDir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$newFilename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$targetPath = $attachDir.'/'.$newFilename;
rename($tempPath,$targetPath);
@chmod($targetPath,0755);
$targetPath=jsonString($targetPath);
if($immediate=='1')$targetPath='!'.$targetPath;
if($msgType==1)$msg="'$targetPath'";
else $msg="{'url':'".$targetPath."','localname':'".jsonString($localName)."','id':'1'}";
}
}
else $err='上传文件扩展名必需为:'.$upExt;
@unlink($tempPath);
}
echo "{'err':'".jsonString($err)."','msg':".$msg."}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
?>本回答被提问者采纳
第2个回答 2016-03-20
upload.php文件内容,如下:
<?php
header('Content-Type: text/html; charset=UTF-8');
$inputName='filedata';//表单文件域name
$attachDir='news';//上传文件保存路径,结尾不要带/
$dirType=1;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
$maxAttachSize=2097152;//最大上传大小,默认是2M
$upExt='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//上传扩展名
$msgType=2;//返回上传参数的格式:1,只返回url,2,返回参数数组
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;
ini_set('date.timezone','Asia/Shanghai');//时区
$err = "";
$msg = "''";
$tempPath=$attachDir.'/'.date("YmdHis").mt_rand(10000,99999).'.tmp';
$localName='';
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5上传
file_put_contents($tempPath,file_get_contents("php://input"));
$localName=urldecode($info[2]);
}
else{//标准表单式上传
$upfile=@$_FILES[$inputName];
if(!isset($upfile))$err='文件域的name错误';
elseif(!empty($upfile['error'])){
switch($upfile['error'])
{
case '1':
$err = '文件大小超过了php.ini定义的upload_max_filesize值';
break;
case '2':
$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
break;
case '3':
$err = '文件上传不完全';
break;
case '4':
$err = '无文件上传';
break;
case '6':
$err = '缺少临时文件夹';
break;
case '7':
$err = '写文件失败';
break;
case '8':
$err = '上传被其它扩展中断';
break;
case '999':
default:
$err = '无有效错误代码';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
else{
move_uploaded_file($upfile['tmp_name'],$tempPath);
$localName=$upfile['name'];
}
}
if($err==''){
$fileInfo=pathinfo($localName);
$extension=$fileInfo['extension'];
if(preg_match('/^('.str_replace(',','|',$upExt).')$/i',$extension))
{
$bytes=filesize($tempPath);
if($bytes > $maxAttachSize)$err='请不要上传大小超过'.formatBytes($maxAttachSize).'的文件';
else
{
switch($dirType)
{
case 1: $attachSubDir = 'day_'.date('ymd'); break;
case 2: $attachSubDir = 'month_'.date('ym'); break;
case 3: $attachSubDir = 'ext_'.$extension; break;
}
$attachDir = $attachDir.'/'.$attachSubDir;
if(!is_dir($attachDir))
{
@mkdir($attachDir, 0777);
@fclose(fopen($attachDir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$newFilename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$targetPath = $attachDir.'/'.$newFilename;
rename($tempPath,$targetPath);
@chmod($targetPath,0755);
$targetPath=jsonString($targetPath);
if($immediate=='1')$targetPath='!'.$targetPath;
if($msgType==1)$msg="'$targetPath'";
else $msg="{'url':'".$targetPath."','localname':'".jsonString($localName)."','id':'1'}";
}
}
else $err='上传文件扩展名必需为:'.$upExt;
@unlink($tempPath);
}
echo "{'err':'".jsonString($err)."','msg':".$msg."}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
?>
<?php
header('Content-Type: text/html; charset=UTF-8');
$inputName='filedata';//表单文件域name
$attachDir='news';//上传文件保存路径,结尾不要带/
$dirType=1;//1:按天存入目录 2:按月存入目录 3:按扩展名存目录 建议使用按天存
$maxAttachSize=2097152;//最大上传大小,默认是2M
$upExt='txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid';//上传扩展名
$msgType=2;//返回上传参数的格式:1,只返回url,2,返回参数数组
$immediate=isset($_GET['immediate'])?$_GET['immediate']:0;
ini_set('date.timezone','Asia/Shanghai');//时区
$err = "";
$msg = "''";
$tempPath=$attachDir.'/'.date("YmdHis").mt_rand(10000,99999).'.tmp';
$localName='';
if(isset($_SERVER['HTTP_CONTENT_DISPOSITION'])&&preg_match('/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i',$_SERVER['HTTP_CONTENT_DISPOSITION'],$info)){//HTML5上传
file_put_contents($tempPath,file_get_contents("php://input"));
$localName=urldecode($info[2]);
}
else{//标准表单式上传
$upfile=@$_FILES[$inputName];
if(!isset($upfile))$err='文件域的name错误';
elseif(!empty($upfile['error'])){
switch($upfile['error'])
{
case '1':
$err = '文件大小超过了php.ini定义的upload_max_filesize值';
break;
case '2':
$err = '文件大小超过了HTML定义的MAX_FILE_SIZE值';
break;
case '3':
$err = '文件上传不完全';
break;
case '4':
$err = '无文件上传';
break;
case '6':
$err = '缺少临时文件夹';
break;
case '7':
$err = '写文件失败';
break;
case '8':
$err = '上传被其它扩展中断';
break;
case '999':
default:
$err = '无有效错误代码';
}
}
elseif(empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none')$err = '无文件上传';
else{
move_uploaded_file($upfile['tmp_name'],$tempPath);
$localName=$upfile['name'];
}
}
if($err==''){
$fileInfo=pathinfo($localName);
$extension=$fileInfo['extension'];
if(preg_match('/^('.str_replace(',','|',$upExt).')$/i',$extension))
{
$bytes=filesize($tempPath);
if($bytes > $maxAttachSize)$err='请不要上传大小超过'.formatBytes($maxAttachSize).'的文件';
else
{
switch($dirType)
{
case 1: $attachSubDir = 'day_'.date('ymd'); break;
case 2: $attachSubDir = 'month_'.date('ym'); break;
case 3: $attachSubDir = 'ext_'.$extension; break;
}
$attachDir = $attachDir.'/'.$attachSubDir;
if(!is_dir($attachDir))
{
@mkdir($attachDir, 0777);
@fclose(fopen($attachDir.'/index.htm', 'w'));
}
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
$newFilename=date("YmdHis").mt_rand(1000,9999).'.'.$extension;
$targetPath = $attachDir.'/'.$newFilename;
rename($tempPath,$targetPath);
@chmod($targetPath,0755);
$targetPath=jsonString($targetPath);
if($immediate=='1')$targetPath='!'.$targetPath;
if($msgType==1)$msg="'$targetPath'";
else $msg="{'url':'".$targetPath."','localname':'".jsonString($localName)."','id':'1'}";
}
}
else $err='上传文件扩展名必需为:'.$upExt;
@unlink($tempPath);
}
echo "{'err':'".jsonString($err)."','msg':".$msg."}";
function jsonString($str)
{
return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
function formatBytes($bytes) {
if($bytes >= 1073741824) {
$bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
} elseif($bytes >= 1048576) {
$bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
} elseif($bytes >= 1024) {
$bytes = round($bytes / 1024 * 100) / 100 . 'KB';
} else {
$bytes = $bytes . 'Bytes';
}
return $bytes;
}
?>