php上傳圖片如何以時間戳記命名 php檔案儲存路徑方法

來源:互聯網
上載者:User
  1. 圖片匯入

複製代碼

2,php代碼(檔案上傳,以時間戳記命名檔案,session中儲存檔案路徑)

  1. /**
  2. *Mwbe Version1.0
  3. *-----------------------------------------------
  4. *Copy 2013-2014 ylt
  5. *Web: communicate
  6. *-----------------------------------------------
  7. *Author: tao *Data: 2014-7-22
  8. */
  9. header("Content-Type:text/html;charset=utf-8");
  10. //step 1 使用$_FILES['pic']["error"] 檢查錯誤
  11. if(isset($_GET["action"])=="img"){
  12. if($_FILES["img"]["error"] > 0){
  13. switch($_FILES["img"]["error"]) {
  14. case 1:
  15. echo "";
  16. break;
  17. case 2:
  18. echo "";
  19. break;
  20. case 3:
  21. echo "";
  22. break;
  23. case 4:
  24. echo "";
  25. break;
  26. default:
  27. echo "";
  28. }
  29. exit;
  30. }
  31. $maxsize=2000000; //50k
  32. //step 2 使用$_FILES["pic"]["size"] 限制大小 單位位元組 2M=2000000
  33. if($_FILES["img"]["size"] > $maxsize ) {
  34. echo "";
  35. exit;
  36. }
  37. //step 3 使用$_FILES["pic"]["type"]或是檔案的副檔名 限制類型 MIME image/gif image/png gif png jpg
  38. /* list($dl, $xl) = explode("/", $_FILES["pic"]["type"]);
  39. if($dl!="image"){
  40. echo "請上傳一個圖片,不充許其它類型檔案";
  41. exit;
  42. }
  43. */ bbs.it-home.org
  44. $allowtype=array("png", "gif", "jpg", "jpeg");
  45. $arr=explode(".", $_FILES["img"]["name"]);
  46. $hz=$arr[count($arr)-1];
  47. if(!in_array($hz, $allowtype)){
  48. echo "";
  49. exit;
  50. }
  51. //step 4 將讓傳後的檔案名稱改名
  52. $filepath="../imgweb/";
  53. $fileimgweb="imgweb/";//為了符合UBB的路徑
  54. $randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;
  55. //將臨時位置的檔案移動到指定的目錄上即可
  56. if(is_uploaded_file($_FILES["img"]["tmp_name"])){
  57. if(move_uploaded_file($_FILES["img"]["tmp_name"],$filepath.$randname)){
  58. echo "";
  59. session_start();
  60. $_SESSION['images'] = $fileimgweb.$randname;
  61. }else{
  62. echo "";
  63. }
  64. }else{
  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.