慕粉3413248
2016-11-20 11:34
<html> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="up.php" method ="post" enctype="multipart/form-data" > 选择您要上传的文件: <input type="hidden" name="MAX_FILE_SIZE" value="10"> <input type="file" name="myFile" accept="image/png" /><br> <input type="submit" value="上传文件" /> </form> </body> </html>
<?php //print_r($_FILES); $filename = $_FILES['myFile']['name']; $type = $_FILES['myFile']['type']; $tmp_name = $_FILES['myFile']['tmp_name']; $size = $_FILES['myFile']['size']; $error = $_FILES['myFile']['error']; if($error == 0){ if(move_uploaded_file($tmp_name, "upload/".$filename)){ echo '文件'.$filename.'上传成功'; }else{ echo "文件上传失败"; } }else{ switch($error){ case 1: echo "文件上传超过了PHP配置文件中upload_max_filesize选项的值"; break; case 2: echo "文件大小超过表单中上传文件大小的限制"; break; case 3: echo "文件部分被上传"; break; case 4: echo "没有文件被上传"; break; case 6: echo "找不到临时文件"; break; case 7: echo "文件写入失败"; break; case 8: echo "上传被php扩展程序中断"; break; } }
<?php header('content-type:text/html;charset=utf-8'); print_r($_FILES); //1.通过$_FILES文件上传变量接收上传文件信息 $fileInfo=$_FILES['myFile']; $filename=$fileInfo['name']; $type=$fileInfo['type']; $tmp_name=$fileInfo['tmp_name']; $size=$fileInfo['size']; $error=$fileInfo['error']; //2.判断下错误号,只有为0或者是UPLOAD_ERR_OK,没有错误发生,上传成功 if($error == UPLOAD_ERR_OK){ if(move_uploaded_file($tmp_name, "uploads/".$filename)){ echo '文件'.$filename.'上传成功'; }else{ echo '文件'.$filename.'上传失败'; } }else{ //匹配错误信息 switch($error){ case 1: echo '上传文件超过了PHP配置文件中upload_max_filesize选项的值'; break; case 2: echo '超过了表单MAX_FILE_SIZE限制的大小'; break; case 3: echo '文件部分被上传'; break; case 4: echo '没有选择上传文件'; break; case 6: echo '没有找到临时目录'; break; case 7: case 8: echo '系统错误'; break; } }
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="doAction1.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value='176942' /> 请选择您要上传的文件:<input type="file" name='myFile' /> <!-- <input type="file" name="myFile" accept="image/jpeg,image/gif,image/png"/><br /> --> <input type="submit" value="上传文件" /> </form> </body> </html>
来来,让我帮你顶上去。
为什么我在前端页面限制大小和类型却仍然能够正常上传???
PHP实现文件上传与下载
43741 学习 · 328 问题
相似问题