单文件上传
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 6:
$tips .= '找不到临时目录';
break;
case 7:
$tips .= '文件写入失败。请检查目录权限';
break;
}
echo "<p>$tips</p>";
}else{
if(is_uploaded_file($tmpName)){
$allow = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
$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='300'>";
}else{
}
}else{
echo '<p>文件类型错误</p>';
}
}else{
echo '<p>上传方式非法</p>';
}
}
}
错误提示
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 6:
$tips .= '找不到临时目录';
break;
case 7:
$tips .= '文件写入失败。请检查目录权限';
break;
}
echo "<p>$tips</p>";
}
文件上传
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
多文件 批量上传
if(isset($_FILES['my_pic'])){
foreach ($_FILES['my_pic']['error'] as $key=>$error){
if($error === UPLOAD_ERR_OK){
$temName = $_FILES['my_pic']['tmp_name'][$key];
$name = $_FILES['my_pic']['name'][$key];
$dest = 'uploads/' . $name
move_uploaded_file($temName, $dest);
//预岚
echo "<img src='$dest' width='300'>";
}
}
}