PHP文件上传的处理方法
PHP文件上传配置文件
序号 |
配置项 |
默认值 |
描述 |
1 |
file_uploads |
On |
使 PHP 支持文件上传 |
2 |
upload_tmp_dir |
/tmp |
指示应该临时把上传的文件存储在什么位置 |
3 |
max_file_uploads |
20 |
单次请求时允许上传的最大文件数量 |
4 |
max_execution_time |
30 |
设置脚本被解析器终止之前PHP最长执行时间(秒) ,防止服务器资源被耗尽 |
5 |
max_input_time |
60 |
设置 PHP 通过 POST/GET/PUT 解析接收数据的时长(秒) |
6 |
memory_limit |
128M |
系统分配给当前脚本执行可用的最大内存容量 |
7 |
post_max_size |
8M |
允许的 POST 数据的总大小 |
8 |
upload_max_filesize |
32M |
允许的尽可能最大的文件上传 |
key:共有 5 个键名, 描述如下:
- name :文件在客户端的原始文件名(即存在用户电脑上的文件名);
- type: 文件的 MIME 类型, 由浏览器提供, PHP 并不检查它;
- tmp_name: 文件被上传到服务器上之后,在临时目录中临时文件名;
- error : 和该文件上传相关的错误代码;
- size : 已上传文件的大小(单位为字);
处理文件上传方式
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
$tmpFileName = $_FILES['my_pic']['tmp_name'];
$originalFileName = $_FILES['my_pic']['name'];
if(is_uploaded_file($tmpFileName))
echo "{$originalFileName}:上传方式合法";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
PHP 文件上传的错误类型
文件上传错误信息描述
序号 |
常量 |
值 |
描述 |
1 |
UPLOAD_ERR_OK |
0 |
没有错误发生,文件上传成功 |
2 |
UPLOAD_ERR_INI_SIZE |
1 |
文件超过php.ini 中upload_max_filesize 值 |
3 |
UPLOAD_ERR_FORM_SIZE |
2 |
文件大小超过表单中MAX_FILE_SIZE 指定的值 |
4 |
UPLOAD_ERR_PARTIAL |
3 |
文件只有部分被上传 |
5 |
UPLOAD_ERR_NO_FILE |
4 |
没有文件被上传 |
6 |
UPLOAD_ERR_NO_TMP_DIR |
6 |
找不到临时文件夹 |
7 |
UPLOAD_ERR_CANT_WRITE |
7 |
文件写入失败 |
根据上传文件的错误类型判断
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
$errorCode = $_FILES['my_pic']['error'];
if($errorCode ===4 ) echo '没有文件上传';
//两种方式检查
//if(empty($_FILES['my_pic']['name'])) echo '没有文件传';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
处理文件类型是否符合要求 要求后缀是 ‘jpg’,’jpeg’,’png’,’wbmp’,’gif’
<?php
//$_FILES;
printf('<pre>%s</pre>',print_r($_FILES,true));
$fileType = $_FILES['my_pic']['type'];
echo $fileType,'<br>';
//print_r(explode('/',$fileType)[0]);
echo strstr($fileType,'/',true);
echo '<br>';
$allow = ['jpg','jpeg','png','wbmp','gif'];
$extension = pathinfo($_FILES['my_pic']['name'])['extension'];
echo $extension;
if(!in_array($extension,$allow)){echo '文件类型不正确';}
else{
echo '合法的文件类型';
}
//if(strstr($fileType,'/',true) !== 'image')
//echo '<p>文件类型错误</p>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<hr>
<!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传:检测文件类型</legend>
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
检查文件上传内容不超过5M
用ini_get(‘uplond_max_filesize’)检测文件上传的最大值,将配置文件里的上传最大值改为5M;
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
echo ini_get('upload_max_filesize');
ini_set('upload_max_filesize','5M');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="5524880">
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
PHP文件上传,多文件上传:逐个上传
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
foreach ($_FILES as $file) {
if($file['error'] == 0)
{
$destFile = 'upload/'.$file['name'];
move_uploaded_file($file['tmp_name'],$destFile);
echo "<img src='{$destFile}' width ='150'>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
</head>
<body>
<!-- <form action="" method="post" enctype="application/x-www-form-urlencoded"> -->
<form action="" method="post" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传:逐个上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="5524880">
<input type="file" name="my_pic1">
<input type="file" name="my_pic2">
<input type="file" name="my_pic3">
<button>上传</button>
</fieldset>
</form>
</body>
</html>