php上傳圖片之時間戳記命名(儲存路徑)_php技巧

來源:互聯網
上載者:User

html代碼:

<div id="images" style="width:250px;height:120px;background:#fff;border:1px solid #ccc;">    <h2><strong>圖片匯入</strong></h2>   <form enctype="multipart/form-data" action="./includer/importimg.inc.php?action=img" method="post" name="imge">        <input type="hidden" name="MAX_FILE_SIZE" value="100000000" />  <input value="匯入檔案" type="file" name="img" id="file"/><br/><br/>  <input type="submit" id="imgbut" class="buttons" value="上傳圖片" />                                            </form> </div>

php代碼:

<?php/***Mwbe Version1.0*-----------------------------------------------*Copy 2013-2014 ylt*Web: communicate*-----------------------------------------------*Author: tao *Data: 2014-7-22*/header("Content-Type:text/html;charset=utf-8");//step 1 使用$_FILES['pic']["error"] 檢查錯誤if(isset($_GET["action"])=="img"){if($_FILES["img"]["error"] > 0){  switch($_FILES["img"]["error"]) {  case 1:    echo "<script type='text/javascript'>alert('上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值<br>');history.back();</script>";    break;  case 2:    echo "<script type='text/javascript'>alert('上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值');history.back();</script>";    break;  case 3:    echo "<script type='text/javascript'>alert('檔案只有部分被上傳');history.back();</script>";    break;  case 4:    echo "<script type='text/javascript'>alert('沒有檔案被上傳');history.back();</script>";    break;  default:    echo "<script type='text/javascript'>alert('末知錯誤');history.back();</script>";  }  exit;}$maxsize=2000000; //50k//step 2 使用$_FILES["pic"]["size"] 限制大小 單位位元組 2M=2000000if($_FILES["img"]["size"] > $maxsize ) {  echo "<script type='text/javascript'>alert('上傳的檔案太大,不能超過{$maxsize}位元組');history.back();</script>";  exit;}//step 3 使用$_FILES["pic"]["type"]或是檔案的副檔名 限制類型 MIME image/gif image/png  gif png jpg /*  list($dl, $xl) = explode("/", $_FILES["pic"]["type"]); if($dl!="image"){ echo "請上傳一個圖片,不充許其它類型檔案"; exit; } */ $allowtype=array("png", "gif", "jpg", "jpeg"); $arr=explode(".", $_FILES["img"]["name"]); $hz=$arr[count($arr)-1]; if(!in_array($hz, $allowtype)){ echo "<script type='text/javascript'>alert('這是不允許的類型');history.back();</script>";exit; } //step 4 將讓傳後的檔案名稱改名 $filepath="../imgweb/"; $fileimgweb="imgweb/";//為了符合UBB的路徑$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;  //將臨時位置的檔案移動到指定的目錄上即可  if(is_uploaded_file($_FILES["img"]["tmp_name"])){    if(move_uploaded_file($_FILES["img"]["tmp_name"],$filepath.$randname)){      echo "<script type='text/javascript'>history.back();</script>";      session_start();      $_SESSION['images'] = $fileimgweb.$randname;      }else{      echo "<script type='text/javascript'>alert('上傳失敗');history.back();</script>";      }}else{echo"<script type='text/javascript'>alert('不是一個上傳檔案');history.back();</script>";}}?>

聯繫我們

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