使用PHP接受檔案並獲得其尾碼名的方法_php技巧

來源:互聯網
上載者:User

HTML的form表單
用html的表單類比一個檔案上傳的post請求,代碼如下:

  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   <html>   <head>   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   <title>File Upload</title>   </head>   <body>      <form enctype="multipart/form-data" action="test.php" method="POST">     <input type="hidden" name="MAX_FILE_SIZE" value="30000" />     Send this File:<input name="userfile" type="file"/>     <input type="submit" value="Send File" />   </form>         </body>   </html> 


注意:

要確保檔案上傳表單的屬性是 enctype="multipart/form-data",否則檔案上傳不了


PHP
首先,需要解釋一下PHP的全域變數$_FILES,此數組包含了所有上傳的檔案資訊

  • $_FILE['userfile']['name'] : 用戶端機器檔案的原名稱
  • $_FILE['userfile']['type'] : 檔案的MIME類型
  • $_FILE['userfile']['size'] : 已上傳的檔案大小
  • $_FILE['userfile']['tmpname'] : 檔案被上傳後在伺服器儲存的臨時檔案名稱
  • $_FILE['userfile']['error'] : 和該檔案上傳的錯誤碼


思路
1、產生40位的隨機字串作為檔案名稱
2、根據檔案是圖片還是語音轉存到不同的檔案位置
3、暫時不做檔案大小和檔案類型的校正

 

  function processFile($files, $type) {     $uploadName = null;     foreach ($files as $name => $value) {       $originalName = $value['name'];       $arr = explode(".", $originalName);       $postfix = $arr[count($arr) - 1];       $tmpPath = $value['tmp_name'];       $tmpType = $value['type'];       $tmpSize = $value['size'];     }          $newname = EhlStaticFunction::generateRandomStr(40).".".$postfix;          switch ($type) {       case 1 :          // 處理音效檔         $destination = VIDEOUPLOADDIR.$newname;         break;       case 2 :         // 處理影像檔         $destination = IMAGEUPLOADDIR.$newname;         break;     }          move_uploaded_file($tmpPath, $destination);   } 

而擷取所上傳檔案的尾碼名則可以使用一下代碼:

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title></title>  <meta name="keywords" content=" keywords" />  <meta name="description" content="description" /></head><body>  <form method="post" action="" enctype="multipart/form-data">  <input type="file" name="upfile" size="20" />  <input type="submit" name="submit" value="submit" />  </form></body></html>


PHP

<?PHP  if(isset($_POST['submit'])) {    $string = strrev($_FILES['upfile']['name']);    $array = explode('.',$string);    echo $array[0];  }  ?>

結果樣本:

聯繫我們

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