单文件上传
<?php
printf('<pre>%s</pre>', print_r($_FILES,true));
if (isset($_FILES['my_pic'])){
// 原始文件名
$name = $_FILES['my_pic']['name'];
// 临时文件名
$tmpName = $_FILES['my_pic']['tmp_name'];
// 错误代码
$error = $_FILES['my_pic']['error'];
if ($error >0) {
$tips = '<span style = "color:red">上传失败</span><br>';
switch($error){
case 1:
$tips = '大小超过了php.ini中的允许上传的文件大小';
break;
case 2:
$tips = '大小超过了表单中(MAX_FILE_SIZE)的允许上传的文件大小';
break;
case 3:
$tips = '文件只有部分被上传';
break;
case 4:
$tips = '没有文件被上传';
break;
case 5:
$tips = '找不到临时目录';
break;
case 6:
$tips = '文件写入失败,请检查目录权限';
break;
}
echo "<p>$tips</p>";
} else{
// 判断上传方式是否合法
if(is_uploaded_file($tmpName)){
// 文件类型白名单
$allow = ['jpg', 'jpeg', 'png', ''];
// 扩展名
$ext = pathinfo($name)['extension'];
if (in_array($ext, $allow)){
// 如果上传方式合法,且是允许的类型,则可以移动到指定的目录中了
$path = 'uploads/';
// 为了防止同名覆盖,应该给目标文件名重命名
$dest = $path . md5($name) . '.' . $ext;
// 将文件从临时目录移动到目标目录中
if(move_uploaded_file($tmpName,$dest)){
echo '<p>上传成功</p>';
echo "<img src='$dest' width='200'>";
} else{
}
} else{
echo '<p>文件类型错误</p>';
}
} else{
echo '<p>上传方式非法</p>';
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<!--
1. method: POST
2. ecntype: multipart/form-data
-->
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<!-- name="my_pic": 给服务器编程语言用的变量名,$_FILES -->
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="800000"> -->
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
多文件上传-1:逐个上传
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
// foreach
foreach($_FILES as $file){
// 只要判断$file['error'] ===0 表示上传成功
if($file['error']===0){
// 暂不加密目标文件名
$dest = 'uploads/' . $file['name'];
// 移动到目标目录中
move_uploaded_file($file['tmp_name'], $dest);
// 预览
echo "<img src= '$dest' width='200'>";
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>多文件上传-1</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传-1:逐个上传</legend>
<input type="file" name="my_pic1">
<input type="file" name="my_pic2">
<input type="file" name="my_pic3">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
多文件上传-2:逐个上传
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
if(isset($_FILES['my_pic'])){
// foreach
foreach($_FILES['my_pic']['error'] as $key=>$error){
// 0 => UPLOAD_ERR_OK
if($error === UPLOAD_ERR_OK){
// 临时文件名
$tmpName =$_FILES['my_pic']['tmp_name'][$key];
// 原始文件名
$Name =$_FILES['my_pic']['name'][$key];
// 暂不加密目标文件名
$dest = 'uploads/' . $name;
// 移动到目标目录中
move_uploaded_file($tmpName, $dest);
// 预览
echo "<img src= '$dest' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>多文件上传-2</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传-2:逐个上传</legend>
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
多文件上传-3:批量上传
<?php
// $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
if(isset($_FILES['my_pic'])){
// foreach
foreach($_FILES['my_pic']['error'] as $key=>$error){
// 0 => UPLOAD_ERR_OK
if($error === UPLOAD_ERR_OK){
// 临时文件名
$tmpName =$_FILES['my_pic']['tmp_name'][$key];
// 原始文件名
$Name =$_FILES['my_pic']['name'][$key];
// 暂不加密目标文件名
$dest = 'uploads/' . $name;
// 移动到目标目录中
move_uploaded_file($tmpName, $dest);
// 预览
echo "<img src= '$dest' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>多文件上传-3</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传-3:批量上传</legend>
<input type="file" name="my_pic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>