单文件:
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
//上传验证:
//是否有文件被上传?
$errorcode=$_FILES['pic']['error'];
//$errorcode=1:文件超过php.ini设置的最大值,见26行;
//$errorcode=2:文件大小超过前端:<input type="hidden" name="MAX_FILE_SIZE" value="500000">的值
switch ($errorcode){
case 1:
echo '文件超过php.ini中upload_max_filesize的值';
break;
case 2:
echo '文件大小超过表达隐藏域中MAX_FILE_SIZE指定的值';
}
if(UPLOAD_ERR_NO_FILE===$errorcode) echo '<p>没有文件被上传</p>';
//是否通过合法POST途径上传?
$filename=$_FILES['pic']['tmp_name'];
$sourcename=$_FILES['pic']['name'];
if(is_uploaded_file($filename)) echo "{$sourcename}:上传方式合法";
//文件类型检查?
$filetype=$_FILES['pic']['type'];
if(strstr($filetype,'/',true)!=='image') echo '<p>文件类型错误</p>';
//设置上传文件的大小:需要修改ini文件:upload_max;路径:xampp1/az/php/php.ini
//文件上传的异常
class UploadException extends Exception{
public function __toString(){
return <<<UPLOAD
<style>
table{border-collapse:collapse;border:1px solid black;text-align:center;}
td{border:1px solid black;padding:5px;}
tr:first-of-type{background-color:#eee;}
tr:last-of-type td {color:coral;}
</style>
<table>
<tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
<tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
</table>
UPLOAD;
}
}
try{
$errorcode=$_FILE['pic']['error'];
if($errorcode>UPLOAD_ERR_OK){
swith($errorcode){
case UPLOAD_ERR_INI_SIZE:
throw new UploadException('文件超过php.ini中upload_max_filesize的值',1);//注意:这个1是给自己看的,随便写几;
break;
case UPLOAD_ERR_FORM_SIZE:
throw new UploadException('文件大小超过表达隐藏域中MAX_FILE_SIZE指定的值',2);//注意:这个1是给自己看的,随便写几;
break;
case UPLOAD_ERR_PARTIAL:
throw new UploadException('文件只有部分上传',3);//注意:这个1是给自己看的,随便写几;
break;
case UPLOAD_ERR_NO_FILE:
throw new UploadException('没有文件被上传',4);//注意:这个1是给自己看的,随便写几;
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new UploadException('找不到临时文件夹',6);//注意:这个1是给自己看的,随便写几;
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败',7);//注意:这个1是给自己看的,随便写几;
break;
default:
throw new UploadException('未知类型错误',8);
}
}
$filetype=$_FILES['pic']['type'];
if(strstr($filetype,'/',true)!=='image') throw new UploadException('文件类型错误',9);
}catch (UploadException $e){
echo $e;
};
if(is_uploaded_file($filename)){
$result='uploads/'.md5(time().mt_rand(1,1000)).strstr($sourcename,'.');
if(move_uploaded_file($filename,$result)){
echo "<p>$sourcename:上传成功~~</p>";
echo "<img src='{$result}' width='200'>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device_width,initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- 必须post -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
//多文件上传
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
if($_FILES['pic']){
foreach ($_FILES['pic']['error'] as $k=>$v){
if($v===UPLOAD_ERR_OK){
$tmp=$_FILES['pic']['tmp_name'][$k];
$source=$_FILES['pic']['name'][$k];
$filename='uploads/'.md5(time().mt_rand(1,1000)).strstr($source,'.');
move_uploaded_file($tmp,$filename);
echo "<img src='{$filename}' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device_width,initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- 必须post -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pic[]">
<input type="file" name="pic[]">
<input type="file" name="pic[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
//批量上传
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
if($_FILES['pic']){
foreach ($_FILES['pic']['error'] as $k=>$v){
if($v===UPLOAD_ERR_OK){
$tmp=$_FILES['pic']['tmp_name'][$k];
$source=$_FILES['pic']['name'][$k];
$filename='uploads/'.md5(time().mt_rand(1,1000)).strstr($source,'.');
move_uploaded_file($tmp,$filename);
echo "<img src='{$filename}' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device_width,initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- 必须post -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>批量上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
<input type="file" name="pic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>