使用 PHP 上傳檔案

文章位置: 主頁 > 文章資料庫 > PHP 教學 > 使用 PHP 上傳檔案
瀏覽次數: 20061
更新時間: 2005/05/08 20:38

建文 HTML 表單

要製作用作上傳檔案的表單與傳統的表單差不多,但必須將表單中的 enctype 屬性設成 multipart/form-data。enctype 屬性是指定瀏覽器對表單中的參數進行編碼的方法,預設值為 application/x-www-form-urlencoded。
以下是建立表單的例子:

upload.html
01 <html>
02 <head>
03 <title>A simple upload form</title>
04 </head>
05 <body>
06 <h2>A simple upload form</h2>
07
08 <form method="post" action="upload.php" enctype="multipart/form-data">
09 Enter file name: <input type="file" name="userfile"><br>
10 <input type="submit" value="Upload">
11 </form>
12
13 </body>
14 </html>



處理上傳檔案

建立了以上的 HTMl 表單後,下一步要做的就是後端的處理。在 PHP 中可以透過二維陣列 $HTTP_POST_FILES 或 $_FILES 來存取所上傳的檔案詳細內容。就以以上表單為例,輸入元素名為 userfile,各陣列元素為:

$_FILES['userfile']['name'] -- 檔案在客戶端電腦上的檔案名稱

$_FILES['userfile']['type'] -- 檔案的 MIME 類型,例如 "image/gif"

$_FILES['userfile']['size'] -- 上傳檔案的檔案大小,單為為 bytes

$_FILES['userfile']['tmp_name'] -- 上傳檔案儲存在伺服器端的暫存檔案名

$_FILES['userfile']['error'] -- 在 PHP 4.2.0 或更新的片本才有的這個陣列元素,上傳檔案的錯誤號碼


上傳檔案的 PHP 程式

以上介紹了建立上傳檔案用的表單及存取陣列,以下便建立上傳檔案的程式。首先需要建立一個目錄作儲存檔案,並需要確定 web server 有寫入的權限,一般設成 777 就可以,假設使用以上的上傳表單,以及上傳目錄是 "/var/www/uploads/",以下是程式碼範例會顯示上傳檔案資訊以及上傳檔案:

upload.php
01 <html>
02 <head>
03 <title>A simple upload form</title>
04 </head>
05 <body>
06 <h2>Uploaded File Details</h2>
07
08 <?php
09
// display file details
10
echo "Filename: " $_FILES['userfile']['name'] . "<br>";
11 echo 
"Temporary Name: " $_FILES['userfile']['tmp_name'] . "<br>";
12 echo 
"Size: "$_FILES['userfile']['size'] . "<br>";
13 echo 
"Type: "$_FILES['userfile']['type'] . "<br>";
14
15
// copy file here
16
if(@copy($_FILES['userfile']['tmp_name'], "/var/www/uploads/" $_FILES['userfile']['name'])){
17     echo 
"<b>File successfully upload</b>";
18 }else{
19     echo 
"<b>Error: failed to upload file</b>";
20 }
21
?>
22

23 </body>
24 </html>


以上傳子使用了 copy 函式上傳檔案,其實 PHP 對上傳檔案已提供了兩個專用函式,它們是 move_uploaded_file() 及 is_uploaded_file(),有興趣的朋友可參考 PHP 使用手冊關於這兩個函式的簡介及用法。


程式的改進

以上的上傳程式只做了最基本的上傳動作,在實際應用中還是會有問題,例如不能容納相同的檔案名稱、同一目錄檔案過多時存取效率減低等問題。但本文只作一個入門的介紹,希望日後可以寫一篇更深入的文章作介紹。


====================================================
歡迎轉載,但轉載時請保留此宣告,不得作為商業用途
作者: Sam Tang <admin{at}phpini{dot}com>
來源網站: http://www.phpdc.com/


用戶評論 按這裡發表新評論 
Johnny user at yourdomain dat com
11 August 2005 21:32
真係得wo@W@
 
小將義 tmlodgar at yahoo dat com dat tw
06 September 2005 23:37
請問那個 temp 目錄 如果是在windows作業系統下,可以寫成 c:\php\temp 嗎?
在php.ini中設定
upload_tmp_dir = c:\php\temp
 
go user at yourdomain dat com
03 November 2005 21:50

 
lesbian cheerleaders ejpeievam47 at mail dat com
14 May 2008 16:16
<a href='http://www.google.com/notebook/public/00684390687061701472/BDSVoIgoQlPTb650j' >wet lesbians</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDQGpIgoQz5vV650j' >lesbian sex</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDT3iIgoQqY6I650j' >ebony lesbians</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDQ6oIgoQ6fTA650j' >lesbo101</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDQqRIwoQqOGM7J0j' >ebony lesbian</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDQ9jIgoQue-m650j' >lesbian sisters</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDQ9jIgoQ-oCU650j' >teenage lesbian</a>
http://www.google.com/notebook/public/00684390687061701472/BDSVoIgoQlPTb650j
http://www.google.com/notebook/public/00684390687061701472/BDQGpIgoQz5vV650j
http://www.google.com/notebook/public/00684390687061701472/BDT3iIgoQqY6I650j
http://www.google.com/notebook/public/00684390687061701472/BDQ6oIgoQ6fTA650j
http://www.google.com/notebook/public/00684390687061701472/BDQqRIwoQqOGM7J0j
http://www.google.com/notebook/public/00684390687061701472/BDQ9jIgoQue-m650j
 
lesbos raojn at mail dat com
14 May 2008 16:16
<a href='http://www.google.com/notebook/public/00684390687061701472/BDR24IgoQyrLZ650j' >lesbian bondage</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDRteIgoQ5cqE7J0j' >lesbos</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDRepIgoQxOeY7J0j' >young lesbians</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDR24IgoQheuy650j' >lesbian hentai</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDR24IgoQqIeQ7J0j' >teen lesbian</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDSUCIwoQifTh650j' >horny lesbians</a> | <a href='http://www.google.com/notebook/public/00684390687061701472/BDT3iIgoQx8WU7J0j' >teen lesbians</a>
http://www.google.com/notebook/public/00684390687061701472/BDR24IgoQyrLZ650j
http://www.google.com/notebook/public/00684390687061701472/BDRteIgoQ5cqE7J0j
http://www.google.com/notebook/public/00684390687061701472/BDRepIgoQxOeY7J0j
http://www.google.com/notebook/public/00684390687061701472/BDR24IgoQheuy650j
http://www.google.com/notebook/public/00684390687061701472/BDR24IgoQqIeQ7J0j
http://www.google.com/notebook/public/00684390687061701472/BDSUCIwoQifTh650j
 
pre teen boy erection akdutaovoo48 at lycosemail dat com
14 May 2008 16:10
<a href='http://www.1url.in/25230' >milf hunting</a> | <a href='http://www.1url.in/25210' >horny mom</a> | <a href='http://www.1url.in/25170' >shemale porn</a> | <a href='http://www.1url.in/25224' >gay teens</a> | <a href='http://www.1url.in/25237' >pre teen model</a> | <a href='http://www.1url.in/25146' >free teen porn</a> | <a href='http://www.1url.in/25128' >shemale movies</a>
http://www.1url.in/25230
http://www.1url.in/25210
http://www.1url.in/25170
http://www.1url.in/25224
http://www.1url.in/25237
http://www.1url.in/25146
 
teen nudist bwsuch at usa dat net
14 May 2008 16:12
<a href='http://www.1url.in/25147' >shemalecocktail</a> | <a href='http://www.1url.in/25171' >lesbian toons</a> | <a href='http://www.1url.in/25268' >milf fucking</a> | <a href='http://www.1url.in/25235' >toons have sex</a> | <a href='http://www.1url.in/25273' >escorts shemale</a> | <a href='http://www.1url.in/25216' >funny toons</a> | <a href='http://www.1url.in/25217' >drunk mom</a>
http://www.1url.in/25147
http://www.1url.in/25171
http://www.1url.in/25268
http://www.1url.in/25235
http://www.1url.in/25273
http://www.1url.in/25216
 
adipex dosage iasspjbo at gmx dat net
12 May 2008 01:47
Site - very comprehensive and meticulous from all points of view, its good!
Just excellent website, I'm sure!
<a href='http://adipexadipexonl.blog.ijijiji.com'>adipex</a>
http://adipexadipexonl.blog.ijijiji.com
 
adipex euujeibu at gmail dat com
12 May 2008 01:48
More or less nothing seems worth doing, but oh well.
I just don't have anything to say now.
<a href='http://adipexadipexonl.blog.ijijiji.com'>adipex</a>
http://adipexadipexonl.blog.ijijiji.com
 
 
 發表評論
姓名:
Email:
內容: