博客列表 >PHP实战:文件上传

PHP实战:文件上传

李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰原创
2020年05月16日 14:45:40985浏览

文件上传代码练习:

单文件上传

  1. <?php
  2. // printf("<pre>%s</pre>",print_r($_FILES,true));
  3. class upException extends Exception
  4. {
  5. public function __toString()
  6. {
  7. return '错误信息:'.$this->message.';错误代码:'.$this->code;
  8. }
  9. }
  10. try{
  11. // echo $type;
  12. // echo $name,$type,$tmp_name,$error,$size;
  13. switch($error)
  14. {
  15. case UPLOAD_ERR_OK:
  16. // var_dump(trim(strstr($type,'/'),'/'));
  17. // var_dump(trim(strstr($type,'/',true),'/')!=='jpeg');
  18. $ty=trim(strstr($type,'/'),'/');
  19. if($ty==='jpeg'){
  20. if(file_exists($tmp_name)&&is_uploaded_file($tmp_name))
  21. {
  22. $obj_type=$ty;
  23. $obj_file='images/'.time().'.'.$obj_type;
  24. move_uploaded_file($tmp_name,$obj_file);
  25. }
  26. exit("<script>alert('上传成功');location.href='demo1.php'</script>");
  27. }else{
  28. exit("<script>alert('格式不正确,请重新上传!');location.href='demo1.php'</script>");
  29. }
  30. break;
  31. case UPLOAD_ERR_INI_SIZE:
  32. throw new upException('文夹过大,超过php限制',1);
  33. break;
  34. case UPLOAD_ERR_FORM_SIZE:
  35. throw new upException('文件过大,超过表单限制',2);
  36. break;
  37. case UPLOAD_ERR_PARTIAL:
  38. throw new upException('上传文件不完整',4);
  39. break;
  40. case UPLOAD_ERR_NO_FILE:
  41. throw new upException('没有文夹上传',5);
  42. break;
  43. case UPLOAD_ERR_NO_TMP_DIR:
  44. throw new upException('无临时目录',6);
  45. break;
  46. case UPLOAD_ERR_CANT_WRITE:
  47. throw new upException('权限不够,文件写入失败',7);
  48. break;
  49. default:
  50. echo "未知错误";
  51. }}catch(upException $e){
  52. echo $e;
  53. }

多文件上传

  1. <?php
  2. // printf("<pre>%s</pre>",print_r($_FILES,true));
  3. class upException extends Exception
  4. {
  5. public function __toString()
  6. {
  7. return '错误信息:'.$this->message.';错误代码:'.$this->code;
  8. }
  9. }
  10. foreach($_FILES as $file){
  11. extract($file);
  12. // echo $name;
  13. try{
  14. // echo $type;
  15. // echo $name,$type,$tmp_name,$error,$size;
  16. switch($error)
  17. {
  18. case UPLOAD_ERR_OK:
  19. // var_dump(trim(strstr($type,'/'),'/'));
  20. // var_dump(trim(strstr($type,'/',true),'/')!=='jpeg');
  21. $ty=trim(strstr($type,'/'),'/');
  22. if($ty==='jpeg'){
  23. if(file_exists($tmp_name)&&is_uploaded_file($tmp_name))
  24. {
  25. $obj_type=$ty;
  26. $obj_file='images/'.trim($name,'.jpg').time().'.'.$obj_type;
  27. // echo $obj_file,'<br>';
  28. move_uploaded_file($tmp_name,$obj_file);
  29. }
  30. }else{
  31. echo ("<script>alert('格式不正确,请重新上传!');location.href='demo2.php'</script>");
  32. }
  33. break;
  34. case UPLOAD_ERR_INI_SIZE:
  35. throw new upException("{$name}文夹过大,超过php限制",1);
  36. break;
  37. case UPLOAD_ERR_FORM_SIZE:
  38. throw new upException("{$name}文件过大,超过表单限制",2);
  39. break;
  40. case UPLOAD_ERR_PARTIAL:
  41. throw new upException("{$name}上传文件不完整",4);
  42. break;
  43. case UPLOAD_ERR_NO_FILE:
  44. throw new upException("{$name}没有文夹上传",5);
  45. break;
  46. case UPLOAD_ERR_NO_TMP_DIR:
  47. throw new upException("{$name}无临时目录",6);
  48. break;
  49. case UPLOAD_ERR_CANT_WRITE:
  50. throw new upException("{$name}权限不够,文件写入失败",7);
  51. break;
  52. default:
  53. echo "未知错误";
  54. }
  55. // exit("<script>alert('上传成功');location.href='demo2.php'</script>");
  56. }catch(upException $e){
  57. echo ("<script>alert('{$e}');</script>");
  58. }}
  59. exit("<script>alert('上传完毕!');location.href='demo2.php'</script>");

批量上传:

  1. <?php
  2. // printf("<pre>%s</pre>",print_r($_FILES,true));
  3. // die();
  4. class upException extends Exception
  5. {
  6. public function __toString()
  7. {
  8. return '错误信息:'.$this->message.';错误代码:'.$this->code;
  9. }
  10. }
  11. $files=$_FILES['my_file'];
  12. foreach($files['error'] as $key=>$value){
  13. try{
  14. switch($value)
  15. {
  16. case UPLOAD_ERR_OK:
  17. // var_dump(trim(strstr($type,'/'),'/'));
  18. // var_dump(trim(strstr($type,'/',true),'/')!=='jpeg');
  19. $ty=trim(strstr($files['type'][$key],'/'),'/');
  20. if($ty==='jpeg'){
  21. if(file_exists($files['tmp_name'][$key])&&is_uploaded_file($files['tmp_name'][$key]))
  22. {
  23. $obj_type=$ty;
  24. $obj_file='images/'.trim($files['name'][$key],'.jpg').time().'.'.$obj_type;
  25. // echo $obj_file,'<br>';
  26. move_uploaded_file($files['tmp_name'][$key],$obj_file);
  27. }
  28. }else{
  29. echo ("<script>alert('{$files['name'][$key]}格式不正确,请重新上传!');</script>");
  30. }
  31. break;
  32. case UPLOAD_ERR_INI_SIZE:
  33. throw new upException("{$files['name'][$key]}文夹过大,超过php限制",1);
  34. break;
  35. case UPLOAD_ERR_FORM_SIZE:
  36. throw new upException("{$files['name'][$key]}文件过大,超过表单限制",2);
  37. break;
  38. case UPLOAD_ERR_PARTIAL:
  39. throw new upException("{$files['name'][$key]}上传文件不完整",4);
  40. break;
  41. case UPLOAD_ERR_NO_FILE:
  42. throw new upException("{$files['name'][$key]}没有文夹上传",5);
  43. break;
  44. case UPLOAD_ERR_NO_TMP_DIR:
  45. throw new upException("{$files['name'][$key]}无临时目录",6);
  46. break;
  47. case UPLOAD_ERR_CANT_WRITE:
  48. throw new upException("{$files['name'][$key]}权限不够,文件写入失败",7);
  49. break;
  50. default:
  51. echo "未知错误";
  52. }}catch (upException $e){
  53. echo ("<script>alert('{$e}');</script>");
  54. }}
  55. exit("<script>alert('上传完毕!');location.href='demo3.php'</script>");

演示结果



总结:

1、上传文件信息关键字:$_FILES(name、type、tmp_name、error、size);
2、把上传文夹从临时目录移动过到目标目录:move_uploaded_file('tmp_name','目标文夹目录')
3、is_uploaded_file();判断上传文件的合法性
4、文件前端以表单的形式上传:

  1. <form action="act3.php" method="POST" enctype="multipart/form-data">
  2. //文件上传,上传方式:post,上传编码:multipart/form-data
  3. <fieldset>
  4. <legend>文件上传</legend>
  5. //隐藏域,用来限制上传文件大小
  6. <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
  7. //上传文件:multiple:多选
  8. <input type="file" name="my_file[]" multiple>
  9. <button>上传</button>
  10. </fieldset>
  11. </form>

5、在上传文件时:需查看设置php.ini中:file_uploadsupload_tmp_dirmax_file_uploadspost_max_sizeupload_max_filesize等相关设置
6、php常用函数:
strstr($str,'分割字符',true|flase):分割字符
trim($str,'去掉的字符'):去掉字符两端的不需要的字符
file_exists($file);检测文件试问存在
7、script函数:
alert('提示内容'):单传提示函数;
location.href='';自动跳转至……;

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议