作业一(单文件上传(仅限pdf上传))
目录
1、html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<form action="demo.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>头像上传</legend>
<input type="hidden" name='MAX_FILE_SIZE' value=''>
<input type="file" name='user_pic'>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
2、demo.php代码:
<?php
// phpinfo();
// print_r($_FILES);
try{
$error_code=$_FILES['user_pic']['error'];
// echo $error_code.'<br>';
// var_dump($error_code);
// echo UPLOAD_ERR_CANT_WRITE;
if ($error_code > UPLOAD_ERR_OK){
switch($error_code){
case UPLOAD_ERR_INI_SIZE:
throw new Exception('文件太大,超过服务器接收大小');
break;
case UPLOAD_ERR_FORM_SIZE:
throw new Exception('文件大小超过网页限制大小');
break;
case UPLOAD_ERR_PARTIAL:
throw new Exception('文件上传不完整');
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new Exception('没有找到可以临时存储文件的目录文件');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new Exception('没有上传权限');
break;
default:
throw new Exception('未知类型错误');
}
}else{
$upfile=$_FILES['user_pic']['tmp_name'];
// echo $upfile;
$type='pdf';
if (is_uploaded_file($upfile)){
$filetype=pathinfo($_FILES['user_pic']['name'])['extension'];
if ($filetype === $type){
$newupname=__DIR__.'\uploadfile'.DIRECTORY_SEPARATOR.md5($_FILES['user_pic']['name']).time().'.'.$filetype;
echo $newupname;
if(move_uploaded_file($upfile,$newupname)){
echo '<script>alert("上传成功");location.href="form.html";</script>';
}
}else{
throw new Exception('文件类型不允许上传,请上传PDF格式');
}
}else{
throw new Exception('上传后,文件非法错误');
}
}
}catch (Exception $e){
echo $e->getMessage();
}
作业二(多文件上传,总上传文件不能大于10M(修改php.ini里面的post_max_size的值为10M即可),且只能传office文档)
1、html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传文件</title>
</head>
<body>
<form action="demo2.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>头像上传</legend>
<!-- <input type="hidden" name='MAX_FILE_SIZE' value=''> -->
<input type="file" name='user_pic[]'>
<input type="file" name='user_pic[]'>
<input type="file" name='user_pic[]'>
<!-- <input type="file" name='user_pic[]'> -->
<button>上传</button>
</fieldset>
</form>
</body>
</html>
2、公共处理函数:代码
<?php
function verify($file){
try{
$error_code=$file[3];
// echo $error_code.'<br>';
// var_dump($error_code);
// echo UPLOAD_ERR_CANT_WRITE;
if ($error_code > UPLOAD_ERR_OK){
switch($error_code){
case UPLOAD_ERR_INI_SIZE:
throw new Exception('文件太大,超过服务器接收大小');
break;
case UPLOAD_ERR_FORM_SIZE:
throw new Exception('文件大小超过网页限制大小');
break;
case UPLOAD_ERR_PARTIAL:
throw new Exception('文件上传不完整');
break;
case UPLOAD_ERR_NO_TMP_DIR:
throw new Exception('没有找到可以临时存储文件的目录文件');
break;
case UPLOAD_ERR_CANT_WRITE:
throw new Exception('没有上传权限');
break;
case UPLOAD_ERR_NO_FILE:
throw new Exception('没有文件被上传');
break;
default:
throw new Exception('未知类型错误');
}
}else{
$upfile=$file[2];
// echo $upfile;
$type=['pdf','doc','txt','xls','ppt','docx','xlsx','pptx'];
if (is_uploaded_file($upfile)){
$filetype=pathinfo($file[0])['extension'];
if (in_array($filetype,$type)){
$newupname=__DIR__.'\uploadfile'.DIRECTORY_SEPARATOR.md5($file[0]).time().'.'.$filetype;
// echo $newupname;
if(move_uploaded_file($upfile,$newupname)){
echo "{$file[0]}:文件上传成功";
}
}else{
throw new Exception("{$file[0]}文件类型不允许上传,请上传office办公文档");
}
}else{
throw new Exception('上传后,文件非法错误');
}
}
}catch (Exception $e){
echo $e->getMessage();
}
}
3、demo2.php代码:
<?php
require 'upfunction.php';
// phpinfo();
// print_r($_FILES);
$offices=[];
//
foreach ($_FILES as $file) {
// print_r($file);
for($i=0;$i<count($file['name']);$i++) {
$offices[]=[
$file['name'][$i],
$file['type'][$i],
$file['tmp_name'][$i],
$file['error'][$i],
$file['size'][$i]
];
}
}
$i=0;
foreach ($offices as $office) {
// print_r($office);
verify($office);
echo '<br>';
}
作业三
1、知识点:
- 关于上传php.ini中相关的设置:
file_upload:On/Off是否允许文件上传
upload_tmp_dir:文件上传的临时目录
upload_max_filesize:单个文件上传的最大容量
post_max_size:通过POST一次性上传数据的大小,至少要大于upload_max_filesize;
max_file_uploads:单个请求,允许上传的最大的文件的数量
2、超全局常量:$_FILES;
[name] => user_pic.jpg
[type] => image/jpeg
[tmp_name] => /Applications/MAMP/tmp/php/phprqLCR1
[error] => 0
[size] => 20029
错误代码:
- 0: UPLOAD_ERR_OK, 没有错误发生,文件上传成功
- 1: UPLOAD_ERR_INI_SIZE, 上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
- 2: UPLOAD_ERR_FORM_SIZE, 上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
- 3: UPLOAD_ERR_PARTIAL, 文件只有部分被上传(网络临时中断,只有部分上传)
- 4: UPLOAD_ERR_NO_FILE, 没有文件被上传
- 6: UPLOAD_ERR_NO_TMP_DIR, 找不到临时文件目录(文件夹或改动,极少遇到,除非人为恶意)
- 7: UPLOAD_ERR_CANT_WRITE, 文件写入失败, 上传目录无写的权限
3、phpinfo();查询php的版本信息;
4、pathinfo();
Array
(
[dirname] => 0219
[basename] => demo2.php
[extension] => php
[filename] => demo2
)
5、in_array($value,$array)
:判断$value是否在$array中,返回布尔值;
6、is_uploaded_file($tmpFile)
:检测是否是通过POST方式上传的文件, 本质上就是检测临时文件是否是一个合法的文件;
7、move_uploaded_file($tmpFile, $destName)
: 把$tmpFile的临时文件移动到$destName文件中,一般要跟上绝对路径;防止找不到目标文件目录;
8、<script>alert("上传成功");location.href="form1.html";</script>
弹窗提示,并返回指定页面;
10、html标签:
<!-- action:处理上传脚本, method: 必须是POST, multipart/form-data:允许接收表单提交的文件 -->
<form action="demo3.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>头像上传</legend>
<!-- 这个上传文件大小的限制字段,必须写到file字段的前面 -->
<input type="hidden" name="MAX_FILE_SIZE" value="600">
<input type="file" name="user_pic">
<!-- <input type="file" name="user_pic1">
<input type="file" name="user_pic2">
<input type="file" name="user_pic3"> -->
<button>上传</button>
</fieldset>
</form>