PHP上傳多檔案、多圖片的範例程式碼

來源:互聯網
上載者:User
  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 " echo " alt=\"圖片預覽:\r檔案名稱:".$destination."\r上傳時間:\">";
  65. echo "
    ";
  66. }
  67. }
  68. ?>
複製代碼

說明:上傳時,需要上傳兩個,否則會報錯。代碼不是很完善,只是給出一個思路,僅供學習參考。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.