Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
序号 | 配置项 | 默认值 | 描述 |
---|---|---|---|
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 |
允许的尽可能最大的文件上传 |
<?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>
文件上传错误信息描述
序号 | 常量 | 值 | 描述 |
---|---|---|---|
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>
<?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>
<?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
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>