PHP上傳(單個)檔案樣本

來源:互聯網
上載者:User

標籤:

通過 PHP,可以把檔案上傳到伺服器。

建立一個檔案上傳表單

允許使用者從表單上傳檔案是非常有用的。

請看下面這個供上傳檔案的 HTML 表單:

<html><body><form action="upload_file.php" method="post" enctype="multipart/form-data"><label for="attach_file">Filename:</label><input type="file" name="attach_file" id="attach_file_id" /> <br /><input type="submit" name="submit" value="Submit" /></form></body></html>

請留意如下有關此表單的資訊:

<form> 標籤的 enctype 屬性規定了在提交表單時要使用哪種內容類型。在表單需要位元據時,比如檔案內容,請使用 "multipart/form-data"。

<input> 標籤的 type="file" 屬性規定了應該把輸入作為檔案來處理。舉例來說,當在瀏覽器中預覽時,會看到輸入框旁邊有一個瀏覽按鈕。

注釋:允許使用者上傳檔案是一個巨大的安全風險。請僅僅允許可信的使用者執行檔案上傳操作。

建立上傳指令碼

"upload_file.php" 檔案含有供上傳檔案的代碼:

<?phpif ($_FILES["attach_file"]["error"] > 0){echo "Error: " . $_FILES["attach_file"]["error"] . "<br />";}else{echo "Upload: " . $_FILES["attach_file"]["name"] . "<br />";echo "Type: " . $_FILES["attach_file"]["type"] . "<br />";echo "Size: " . ($_FILES["attach_file"]["size"] / 1024) . " Kb<br />";echo "Stored in: " . $_FILES["attach_file"]["tmp_name"];}?>

通過使用 PHP 的全域數組 $_FILES,你可以從客戶電腦向遠程伺服器上傳檔案。

第一個參數是表單的 input name(表單file組件的name值),第二個下標可以是 "name", "type", "size", "tmp_name" 或 "error"。就像這樣:

  • $_FILES["attach_file"]["name"] - 被上傳檔案的名稱
  • $_FILES["attach_file"]["type"] - 被上傳檔案的類型
  • $_FILES["attach_file"]["size"] - 被上傳檔案的大小,以位元組(b)為單位
  • $_FILES["attach_file"]["tmp_name"] - 儲存在伺服器的檔案的臨時副本的名稱
  • $_FILES["attach_file"]["error"] - 由檔案上傳導致的錯誤碼

這是一種非常簡單檔案上傳方式。基於安全方面的考慮,您應當增加有關什麼使用者有權上傳檔案的限制。

上傳限制

在這個指令碼中,我們增加了對檔案上傳的限制。使用者只能上傳 .gif 或 .jpeg 檔案,檔案大小必須小於 20 kb:

<?phpif ((($_FILES["attach_file"]["type"] == "image/gif") || ($_FILES["attach_file"]["type"] == "image/jpeg") || ($_FILES["attach_file"]["type"] == "image/pjpeg")) && ($_FILES["attach_file"]["size"] < 20000)){if ($_FILES["attach_file"]["error"] > 0){echo "Error: " . $_FILES["attach_file"]["error"] . "<br />";}else{echo "Upload: " . $_FILES["attach_file"]["name"] . "<br />";echo "Type: " . $_FILES["attach_file"]["type"] . "<br />";echo "Size: " . ($_FILES["attach_file"]["size"] / 1024) . " Kb<br />";echo "Stored in: " . $_FILES["attach_file"]["tmp_name"];}}else{echo "Invalid file";}?>

注釋:對於 IE,識別 jpg 檔案的類型必須是 pjpeg,對於 FireFox,必須是 jpeg。

儲存被上傳的檔案

上面的例子在伺服器的 PHP 臨時檔案夾建立了一個被上傳檔案的臨時副本。

這個臨時的複製檔案會在指令碼結束時消失。要儲存被上傳的檔案,我們需要把它拷貝到另外的位置:

<?phpif ((($_FILES["attach_file"]["type"] == "image/gif") || ($_FILES["attach_file"]["type"] == "image/jpeg") || ($_FILES["attach_file"]["type"] == "image/pjpeg")) && ($_FILES["attach_file"]["size"] < 20000)){if ($_FILES["attach_file"]["error"] > 0){echo "Return Code: " . $_FILES["attach_file"]["error"] . "<br />";}else{echo "Upload: " . $_FILES["attach_file"]["name"] . "<br />";echo "Type: " . $_FILES["attach_file"]["type"] . "<br />";echo "Size: " . ($_FILES["attach_file"]["size"] / 1024) . " Kb<br />";echo "Temp file: " . $_FILES["attach_file"]["tmp_name"] . "<br />";if (file_exists("upload/" . $_FILES["attach_file"]["name"])){echo $_FILES["attach_file"]["name"] . " already exists. ";}else{move_uploaded_file($_FILES["attach_file"]["tmp_name"], "upload/" . $_FILES["attach_file"]["name"]);echo "Stored in: " . "upload/" . $_FILES["attach_file"]["name"];}}}else{echo "Invalid file";}?>

上面的指令碼檢測了是否已存在此檔案,如果不存在,則把檔案拷貝到指定的檔案夾。

注意:這個例子把檔案儲存到了名為 "upload" 的新檔案夾。

 

延伸閱讀

PHP同時上傳“多個”檔案樣本,並格式化$_FILES數組資訊

【推薦】PHP上傳檔案大小限制大全

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.