PHP上傳檔案詳解

來源:互聯網
上載者:User

標籤:

1.上傳檔案使用的提交方式和請求Content-type

  POST提交方式,原始的form表單提交請加上enctype="multipart/form-data"

 

2.MAX_FILE_SIZE 隱藏欄位(單位為位元組)

  必須放在檔案輸入欄位之前,其值為接收檔案的最大尺寸。這是對瀏覽器的一個建議,PHP 也會檢查此項。在瀏覽器端可以簡單繞過此設定,因此不要指望用此特性來阻擋大檔案。實際上,PHP 設定中的上傳檔案最大值是不會失效的。但是最好還是在表單中加上此項目,因為它可以避免使用者在花時間等待上傳大檔案之後才發現檔案過大上傳失敗的麻煩

 

3.關於上傳檔案的相關配置有哪些?

  file_uploads = On/Off #是否允許檔案上傳

  upload_max_filesize = 20M #上傳檔案的大小限制

  max_file_uploads = 20 #每個請求上傳檔案的數量限制

  max_input_time = 60 #指令碼解析輸入資料允許的最大時間,單位是秒。 它從接收所有資料到開始執行指令碼進行測量的

  post_max_size = 16M #最大POST資料大小

  upload_tmp_dir =/tmp #上傳檔案的臨時目錄

 

4.PHP從$_FILES數組中擷取上傳檔案資訊

  $_FILES[‘userfile‘][‘name‘]  #用戶端機器檔案的原名稱
  $_FILES[‘userfile‘][‘type‘]    #檔案的 MIME 類型,一個例子是“image/gif”。不過此 MIME 類型在 PHP 端並不檢查,因此不要想當然認為有這個值
  $_FILES[‘userfile‘][‘size‘]  #已上傳檔案的大小,單位為位元組
  $_FILES[‘userfile‘][‘tmp_name‘] #檔案被上傳後在服務端儲存的臨時檔案名稱
  $_FILES[‘userfile‘][‘error‘]  #和該檔案上傳相關的錯誤碼。此項目是在 PHP 4.2.0 版本中增加的

  註:如果該檔案沒有被移動到其它地方也沒有被改名,則該檔案將在表單請求結束時被刪除。


5.單檔案上傳執行個體

前端<form enctype="multipart/form-data" action="__URL__" method="POST">    <!-- MAX_FILE_SIZE must precede the file input field -->    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />    <!-- Name of input element determines name in $_FILES array -->    Send this file: <input name="userfile" type="file" />    <input type="submit" value="Send File" /></form>後端<?php// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead// of $_FILES.$uploaddir = ‘/var/www/uploads/‘;$uploadfile = $uploaddir . basename($_FILES[‘userfile‘][‘name‘]);echo ‘<pre>‘;if (move_uploaded_file($_FILES[‘userfile‘][‘tmp_name‘], $uploadfile)) {    echo "File is valid, and was successfully uploaded.\n";} else {    echo "Possible file upload attack!\n";}echo ‘Here is some more debugging info:‘;print_r($_FILES);print "</pre>";

 

6.多檔案上傳

前端<form action="" method="post" enctype="multipart/form-data"><p>Pictures:<input type="file" name="pictures[]" /><input type="file" name="pictures[]" /><input type="file" name="pictures[]" /><input type="submit" value="Send" /></p></form>後端<?phpforeach ($_FILES["pictures"]["error"] as $key => $error) {    if ($error == UPLOAD_ERR_OK) {        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];        $name = $_FILES["pictures"]["name"][$key];        move_uploaded_file($tmp_name, "data/$name");    }}

 

 

7.is_uploaded_file — 判斷檔案是否是通過 HTTP POST 上傳的

  這可以用來確保惡意的使用者無法欺騙指令碼去訪問本不能訪問的檔案,例如 /etc/passwd。  

<?phpif (is_uploaded_file($_FILES[‘userfile‘][‘tmp_name‘])) {   echo "File ". $_FILES[‘userfile‘][‘name‘] ." uploaded successfully.\n";   echo "Displaying contents\n";   readfile($_FILES[‘userfile‘][‘tmp_name‘]);} else {   echo "Possible file upload attack: ";   echo "filename ‘". $_FILES[‘userfile‘][‘tmp_name‘] . "‘.";}

 

PHP上傳檔案詳解

聯繫我們

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