PHP 圖片上傳程式碼範例

來源:互聯網
上載者:User

PHP 檔案上傳程式碼範例

  1. 圖片上傳
  2. $max_file_szie=2*pow(2,20); //上傳的檔案小於2MB
  3. $destination_folder='uploadimg/'; //上傳檔案儲存路徑
  4. if($_SERVER['REQUEST_METHOD']=='POST'){
  5. if(!is_uploaded_file($_FILES['upfile']['tmp_name'])){
  6. echo '圖片不存在!';
  7. exit;
  8. }
  9. if($max_file_szie<$_FILES['upfile']['size']){
  10. echo '檔案太大了!';
  11. exit;
  12. }
  13. if(!in_array($_FILES['upfile']['type'],$uptypes)){
  14. echo '檔案類型不符合!'.$_FILES['upfile']['type'];
  15. exit;
  16. }
  17. if(!file_exists($destination_folder)){
  18. mkdir($destination_folder);
  19. }
  20. $filename=$_FILES['upfile']['tmp_name'];
  21. $image_size=getimagesize($filename);
  22. $pinfo=pathinfo($_FILES['upfile']['name']); //檔案路徑資訊
  23. $ftype=$pinfo['extension']; //舊檔案尾碼名
  24. $destination = $destination_folder.time().".".$ftype; //新檔案名稱
  25. if(file_exists($destination)&&$voerwrie !=true){
  26. echo '同名檔案已經存在了!';
  27. exit;
  28. }
  29. //把上傳的檔案從臨時檔案夾移動到指定目錄
  30. if(!move_uploaded_file($filename,$destination)){
  31. echo '移動檔案出錯了!';
  32. exit;
  33. }
  34. $pinfo=pathinfo($destination);
  35. $fname=$pinfo[basename];
  36. echo "已經成功上傳
    檔案名稱:
  37. ".$destination_folder.$fname."
    ";
  38. echo '寬度:'.$image_size[0];
  39. echo '高度:'.$image_size[1];
  40. echo '
    大小:'.$_FILES['upfile']['size']."bytes";
  41. }
  42. ?>
複製代碼
圖片上傳, PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.