文件上传

-
配置php.ini
image.png
-
$_FILES
image.png
-
文件上传错误代码
image.png
-
文件上传代码
image.png
image.png
-
文件上传
image.png
image.png
image.png
image.png
image.png
- rename
原始文件被删除
[图片上传失败...(image-564c0b-1585800082445)]







images.jianshu.io/upload_images/2294180-5b5745c1ab13deed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

-
多文件上传
image.png


- 多文件上传之文件数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多文件上传</title>
</head>
<body>
<h3>多文件上传之文件数组</h3>
<form action = "post.php" method="post" enctype="multipart/form-data">
<input type = "hidden" name="MAX_FILE_SIZE" value="2000000" /><br />
<input type="file" name="file[]" /><br/>
<input type="file" name="file[]" /><br/>
<input type="submit" value="上传文件"/>
</form>
</body>
</html>
<?php
/**
* Created by PhpStorm.
* User:Melody
* Date:2020/4/1
* Time:下午3:21
*/
define('UPLOAD_DIR', './uploads/');
// 记录上传的错误信息
$errors = [];
if ($_FILES['file']) {
$file = $_FILES['file'];
foreach ($file['error'] as $key => $err) {
if ($err == 0) {
// 判断并保存这个文件
if (is_uploaded_file($file['tmp_name'][$key])) {
if (move_uploaded_file($file['tmp_name'][$key], UPLOAD_DIR . $file['name'][$key])) {
echo '第' . ($key + 1) . '个文件:', $file['name'][$key] . '上传成功<br/>';
} else {
$errors[$key] = '文件保存错误';
}
} else {
$errors[$key] = '不是HTTP POST上传的文件';
}
} else {
$err_str = '文件上传错误';
switch ($err) {
case 1:
$err_str = '上传文件的大小超过了upload_max_filesize设置的值';
break;
case 2:
$err_str = '上传文件的大小超过了表单MAX_FILE_SIZE设置的值';
break;
case 3:
$err_str = '部分文件被上传';
break;
case 4:
$err_str = '没有任何上传文件';
break;
default:
break;
}
$errors[$key] = $err_str;
}
}
echo '<h2>文件上传结果:</h2>';
if ($errors) {
foreach ($errors as $k => $e) {
echo '第' . ($k + 1) . '个文件上传失败,失败原因' . $e . '<br/>';
}
} else {
echo '文件全部上传成功';
}
}
ini_set('display_errors','On');//在此php中显示报错信息,我当时安装php时display_errors默认off,有error都不知道,也可以在php.ini修改
可以用find / -name +文件名 找到php.ini
nginx服务器修改php.ini后需要重启php-fpm才能生效
你要转移的文件夹的权限必须要修改:sudo chmod -R 777 文件夹名称,不然会有Permission denied
先查看php-fpm的master进程号 ---master
ps aux|grep php-fpm
重启php-fpm:
kill -USR2 主进程ID ------------master 进程id
./nginx -t 验证nginx配置文件是否正确
nginx -s reload 修改nginx配置后重新加载配置文件
启动php-fpm:sudo ./php-fpm7.2
/usr/local/php/sbin/php-fpm
- 多文件上传之不同文件名
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多文件上传</title>
</head>
<body>
<h3>多文件上传之不同文件名</h3>
<form action = "post.php" method="post" enctype="multipart/form-data">
<input type = "hidden" name="MAX_FILE_SIZE" value="2000000" /><br />
<input type="file" name="file1" /><br/>
<input type="file" name="file2" /><br/>
<input type="submit" value="上传文件"/>
</form>
</body>
</html>
<?php
/**
* Created by PhpStorm.
* User:Melody
* Date:2020/4/1
* Time:下午3:21
*/
define('UPLOAD_DIR', './uploads/');
// 记录上传的错误信息
$errors = [];
$i = 1;
if ($_FILES) {
$files = $_FILES;
foreach ($files as $key => $file) {
if ($file['error'] == 0) {
// 判断并保存这个文件
if (is_uploaded_file($file['tmp_name'])) {
if (move_uploaded_file($file['tmp_name'], UPLOAD_DIR . $file['name'])) {
echo '第' . ($i + 1) . '个文件:', $file['name'] . '上传成功<br/>';
} else {
$errors[$i] = '文件保存错误';
}
} else {
$errors[$i] = '不是HTTP POST上传的文件';
}
} else {
$err_str = '文件上传错误';
switch ($file['error']) {
case 1:
$err_str = '上传文件的大小超过了upload_max_filesize设置的值';
break;
case 2:
$err_str = '上传文件的大小超过了表单MAX_FILE_SIZE设置的值';
break;
case 3:
$err_str = '部分文件被上传';
break;
case 4:
$err_str = '没有任何上传文件';
break;
default:
break;
}
$errors[$i] = $err_str;
}
$i++;
}
echo '<h2>文件上传结果:</h2>';
if ($errors) {
foreach ($errors as $k => $e) {
echo '第' . ($k + 1) . '个文件上传失败,失败原因' . $e . '<br/>';
}
} else {
echo '文件全部上传成功';
}
}

网友评论