PHP上傳圖片的簡單例子(入門參考)

來源:互聯網
上載者:User
複製代碼

2、admin_upfile.php

  1. date_default_timezone_set('PRC');
  2. //這裡上傳 $upsize判斷上傳檔案的大小
  3. $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] : "/attached/"; //檔案上傳路徑
  4. $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform"; //回傳到上頁面編輯框所在Form的Name
  5. $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"]; //回傳到上頁面編輯框的Name
  6. //轉換根目錄的路徑
  7. if (strpos($uppath, "/") == 0) {
  8. $i = 0;
  9. $thpath = $_SERVER["SCRIPT_NAME"];
  10. $thpath = substr($thpath, 1, strlen($thpath));
  11. while (strripos($thpath, "/") !== false) {
  12. $thpath = substr($thpath, strpos($thpath, "/") + 1, strlen($thpath));
  13. $i = ++$i;
  14. }
  15. $pp = "";
  16. for ($j = 0; $j < $i; ++$j) {
  17. $pp .="../";
  18. }
  19. $uppaths = $pp . substr($uppath, 1, strlen($thpath));
  20. }
  21. $filename = date("y-m-d");
  22. if (is_dir($uppaths . $filename) != TRUE)
  23. mkdir($uppaths . $filename, 0777);
  24. // if(is_dir($filename."/".$ctime)!=TRUE) mkdir($filename."/".$ctime,0777);
  25. $f = $_FILES['file1'];
  26. if ($f["type"] != "image/gif" && $f["type"] != "image/pjpeg" && $f["type"] != "image/jpeg" && $f["type"] != "image/x-png") {
  27. echo "";
  28. //echo $f['type'];
  29. return false;
  30. }
  31. //獲得副檔名
  32. $temp_arr = explode(".", $f["name"]);
  33. $file_ext = array_pop($temp_arr);
  34. $file_ext = trim($file_ext);
  35. $file_ext = strtolower($file_ext);
  36. //新檔案名稱
  37. $new_file_name = md5(date("YmdHis")) . '.' . $file_ext;
  38. //$new_file_name = md5(date("YmdHis") . '_' . rand(10000, 99999)) . '.' . $file_ext;
  39. $dest = $uppaths . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //設定檔案名稱為日期加上檔案名稱避免重複 上傳目錄
  40. $dest1 = $uppath . $filename . "/" . date("ymdhis") . "_" . $new_file_name; //設定檔案名稱為日期加上檔案名稱避免重複
  41. $r = move_uploaded_file($f['tmp_name'], $dest);
  42. if ($f['size'] > 0) {
  43. echo "";
  44. echo "";
  45. }
  46. ?>
複製代碼

3、admin_upload.php

  1. 圖片上傳_bbs.it-home.org
  2. $uppath = isset($_REQUEST["upPath"]) ? $_REQUEST["upPath"] . "/" : "/attached/"; //檔案上傳路徑
  3. $formName = isset($_REQUEST["formName"]) ? $_REQUEST["formName"] : "myform"; //回傳到上頁面編輯框所在Form的Name
  4. $editName = isset($_REQUEST["editName"]) ? $_REQUEST["editName"] : $_REQUEST["editName"]; //回傳到上頁面編輯框的Name
  5. ?>
複製代碼
  • 聯繫我們

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