php上傳與刪除圖片的簡單範例

來源:互聯網
上載者:User
  1. /**
  2. php上傳圖片
  3. link:http://bbs.it-home.org
  4. */
  5. if (!empty($_FILES["img"]["name"])) { //提取檔案域內容名稱,並判斷
  6. $path=”uppic/”; //上傳路徑
  7. if(!file_exists($path))
  8. {
  9. //檢查是否有該檔案夾,如果沒有就建立,並給予最高許可權
  10. mkdir(“$path”, 0700);
  11. }//END IF
  12. //允許上傳的檔案格式
  13. $tp = array(“image/gif”,”image/pjpeg”,”image/jpeg”);
  14. //檢查上傳檔案是否在允許上傳的類型
  15. if(!in_array($_FILES["img"]["type"],$tp))
  16. {
  17. echo “”;
  18. exit;
  19. }//END IF
  20. $filetype = $_FILES['img']['type'];
  21. if($filetype == ‘image/jpeg'){
  22. $type = ‘.jpg';
  23. }
  24. if ($filetype == ‘image/jpg') {
  25. $type = ‘.jpg';
  26. }
  27. if ($filetype == ‘image/pjpeg') {
  28. $type = ‘.jpg';
  29. }
  30. if($filetype == ‘image/gif'){
  31. $type = ‘.gif';
  32. }
  33. if($_FILES["img"]["name"])
  34. {
  35. $today=date(“YmdHis”); //擷取時間並賦值給變數
  36. $file2 = $path.$today.$type; //圖片的完整路徑
  37. $img = $today.$type; //圖片名稱
  38. $flag=1;
  39. }//END IF
  40. if($flag) $result=move_uploaded_file($_FILES["img"]["tmp_name"],$file2);
  41. //特別注意這裡傳遞給move_uploaded_file的第一個參數為上傳到伺服器上的臨時檔案
  42. }//END IF
  43. //這裡再將$img的值寫入到資料庫中對應的欄位
  44. ?>
複製代碼

2、刪除圖片:

  1. $img = //資料庫查詢出相應的資料值
  2. unlink(“uppic/”.$img); //刪除檔案
  3. ?>
複製代碼
  • 聯繫我們

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