单条文件上传
<?php
require $_SERVER['DOCUMENT_ROOT'].'/0511/class/upload.class.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>上传测试</title>
</head>
<body>
<!-- 上传提交属性必须是POST -->
<!-- multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。 -->
<form action="" method='POST' enctype='multipart/form-data'>
<legend>单文件上传测试变量$_FILES;</legend>
<!-- 隐藏域限制上传大小 类型file-->
<input type="hidden" name='MAX_FILE_SIZE' value='200000'>
<input type="file" name='pic'>
<button>上传</button>
</form>
<hr>
<form action="" method='post' enctype="multipart/form-data">
<legend></legend>
</form>
</body>
</html>
单条引入文件upload.class.php
<?php
//使用$_FILES变量来接受数据
printf('<pre>%s</pre>',print_r($_FILES,true));
// echo ini_get('upload_max_filesize');
//检查是否有上传
$error= $_FILES['pic']['error'];
// echo $error;
//没有文件的函数 UPLOAD_ERR_NO_FILE;
//检查文件类型
if($_POST){
if(UPLOAD_ERR_NO_FILE === $error){
echo "没有上传文件<br>";
}
$fileType=$_FILES['pic']['type'];
//strstr — 查找字符串的首次出现(stristr忽略大小写)
//检查是否为POST提交
$temFile=$_FILES['pic']['tmp_name'];//临时文件名
$sourceFile=$_FILES['pic']['name'];//源文件名
// echo $temFile.$sourceFile;
//is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的
if(stristr($fileType,'/',true) !=='image')
{
echo "$sourceFile 非法请求!";
}elseif(is_uploaded_file($temFile)){
echo "$sourceFile 上传成功";
}
// ini_get — 获取一个配置选项的值
//ini_set 临时更改php.ini上传大小
//这里限制大小非常有必要,避免通过浏览器修改上传
ini_set('upload_max_filesize','1M');
switch($error){
case 1:
echo '文件大小不符';
break;
case 2:
echo '文件大小不符';
}
}
单条上传+异常处理
<?php
require 'class/upload.class1.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>单文件上传$_FILES</title>
</head>
<body>
<form action="" method='POST' enctype='multipart/form-data'>
<fieldset>
<legend>单文件上传异常处理</legend>
<input type="hidden" name='MAX_FILE_SIZE' value='200000'>
<input type="file" name='pic'>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
单条上传+异常处理类文件upload1.class.php
<?php
//单文件上传异常类
printf('<pre>%s</pre>',print_r($_FILES,true));
//自定义上传异常类
class UploadException extends Exception
{
//在异常类中可访问并重写Exception中的四个属性。通过__toString()格式化输出
public function __toString()
{
return <<< UPLOAD
<style>
table{border-collapse:collapse;border:1px soild #CCC;text-align:center;}
td{border:1px solid #CCC;padding:1px;}
tr:first-of-type{background:#333;}
tr:last-of-type{color:#DDD;}
</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= $_FILES['pic']['error'];
if($errorCode > UPLOAD_ERR_OK)
{
switch ($errorCode)
{
//case视乎不能用数字表示。
case UPLOAD_ERR_INI_SIZE:
throw new UploadException('文件过大',1);
break;
case UPLOAD_ERR_FORM_SIZE:
throw new UploadException('文件过大');
break;
case UPLOAD_ERR_PARTIAL:
throw new UploadException('文件只有部分上传了');
break;
case UPLOAD_ERR_NO_FILE:
throw new UploadException('没有上传文件',4);
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new UploadException('找不到临时文件');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败');
break;
default:
throw new UploadException('未知错误');
}
}
if($_POST){
$fileType=$_FILES['pic']['type'];
if(strstr($fileType,'/', true) !=='image'){
throw new UploadException('文件类型错误');
}
$temFile=$_FILES['pic']['tmp_name'];//临时
// is_uploaded_file — 判断文件是否是通过 HTTP POST 上传的
if(is_uploaded_file($temFile)){
// 判断是否post请求并移动到指定目录
$sourcFile=$_FILES['pic']['name'];//原文件
//目录路径
$FilePath='uploads/'.md5(time().mt_rand(1,99999)).strstr($sourcFile,'.');//。加密获取扩展名
// echo $FilePath;die;
// move_uploaded_file — 将上传的文件移动到新位置
if(move_uploaded_file($temFile,$FilePath)){
echo " $sourcFile '上传成功'";
echo "<img src='{$FilePath}' width='100px'>";
}
}
}
}catch(UploadException $e){
echo $e;
}
批量上传
<?php
// require 'class/uploads.class.php';
//printf('<pre>%s</pre>',print_r($_FILES,true));
if($_FILES['pic'])
{
foreach($_FILES['pic']['error'] as $key=>$error) {
if($error === UPLOAD_ERR_OK){
$temFile = $_FILES['pic']['tmp_name'][$key];
$sourceFile = $_FILES['pic']['name'][$key];
$PathFile = 'uploads/'.md5(time().mt_rand(1,9999)).strstr($sourceFile,'.');
//移动文件
move_uploaded_file($temFile,$PathFile);
echo "<img src='{$PathFile}' width='100px'> ";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>批量上传图片</title>
</head>
<body>
<form action="" method="POST" enctype='multipart/form-data'>
<fieldset>
<legend>批量上传图片</legend>
<input type="hidden" name='MAX_FILE_SIZE' value='200000'>
<!-- multiple 批量上传属性 -->
<input type="file" name="pic[]" multiple>
<button>上传</button>
</fieldset>
<fieldset>
<legend>单个批量上传图片</legend>
<input type="hidden" name='MAX_FILE_SIZE' value='200000'>
<!-- multiple 批量上传属性 只是少了一个multiple属性 -->
<input type="file" name="pic[]" >
<input type="file" name="pic[]" >
<input type="file" name="pic[]" >
<button>上传</button>
</fieldset>
</form>
</body>
</html>
总结:抄了两天作业也没看懂怎么回事。尤其是批量上传异常处理更加是摸不着头脑该怎么弄了。