当前位置:Gxlcms > PHP教程 > PHP上传多文件、多图片的示例代码

PHP上传多文件、多图片的示例代码

时间:2021-07-01 10:21:17 帮助过:15人阅读

  1. $uptypes=array(
  2. //上传文件的ContentType格式
  3. 'image/jpg',
  4. 'image/jpeg',
  5. 'image/png',
  6. 'image/pjpeg',
  7. 'image/gif',
  8. 'image/bmp',
  9. 'image/x-png',
  10. 'application/msword',//doc
  11. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',//docx
  12. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',//pptx
  13. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',//xlsx
  14. 'text/plain'
  15. );
  16. /**
  17. * php多文件、多图片上传
  18. * by bbs.it-home.org
  19. */
  20. $max_file_size=2000000; //上传文件大小限制, 单位BYTE
  21. $dir="upload/"; //上传文件路径
  22. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  23. {
  24. $file = $_FILES['upfile']['name'];
  25. foreach($file as $key=>$item){
  26. if($item != ''){
  27. if (!is_uploaded_file($_FILES['upfile']['tmp_name'][$key]))//是否存在文件
  28. {
  29. echo "图片不存在!";
  30. exit;
  31. }
  32. if($max_file_size < $_FILES['upfile']['size'][$key])//检查文件大小
  33. {
  34. echo "文件太大!";
  35. exit;
  36. }
  37. if(!file_exists($dir))
  38. {
  39. mkdir($dir);
  40. }
  41. $filename=$_FILES['upfile']['tmp_name'][$key];
  42. $image_size = getimagesize($filename);
  43. $pinfo = pathinfo($file[$key]);
  44. $ftype = $pinfo['extension'];
  45. $destination = $dir.time().$file[$key];
  46. if (file_exists($destination) && $overwrite != true)
  47. {
  48. echo "同名文件已经存在了";
  49. exit;
  50. }
  51. if(!move_uploaded_file ($filename, $destination))
  52. {
  53. echo "移动文件出错";
  54. exit;
  55. }
  56. $pinfo=pathinfo($destination);
  57. $fname=$pinfo['basename'];
  58. echo " 已经成功上传
    文件名: ".$dir.$fname."
    ";
  59. echo " 宽度:".$image_size[0];
  60. echo " 长度:".$image_size[1];
  61. echo "
    大小:".$_FILES['upfile']['size']." bytes";
  62. }
  63. echo "
    图片预览:
    ";
  64. echo "\"图片预览:\r文件名:".$destination."\r上传时间:\"";
  65. echo "
    ";
  66. }
  67. }
  68. ?>

说明: 上传时,需要上传两个,否则会报错。 代码不是很完善,只是给出一个思路,仅供学习参考。

人气教程排行