七月 22 2011

PHP 檔案上傳的處理

Category: ? Admin @ 11:03

要在 PHP 建構的網站中上傳檔案,必須遵循以下幾個步驟:

1. 在網頁中建立一個上傳檔案的表單。

1 <form action="SaveFile.php" method="post" enctype="multipart/form-data">
2    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
3    請輸入要上傳的檔案:<input type="file" name="myFileID" /><br/>
4    <input type="submit" value="上傳" />
5 </form>

 

說明:
(1) form 的 action 屬性指定了要處理上傳檔案的 PHP 程式。

(2) form 的 method 屬性一定要用 "POST" 方法傳送資料,而且必須加上 enctype="multipart/form-data" 屬性,才能正確上傳檔案。

(3) 利用隱藏的 input 欄位,name="MAX_FILE_SIZE" 來限制上傳的檔案大小。

(4) input type="file" 表可以讓使用者輸入或選擇檔案。name 屬性設定的名稱是處理上傳檔案的 PHP 程式所需要的依據。

2. 建立一個接收上傳檔案的 PHP 程式。承上例,此檔名為 SaveFile.php

01 $szUpLoad = "./UpLoad/";
02 if( $_FILES["myFileID"]["error"] == UPLOAD_ERR_OK )
03 {          
04     if( move_uploaded_file($_FILES["myFileID"]["tmp_name"],$szUpLoad.$_FILES["myFileID"]["name"]))
05     {
06         echo "成功";
07     }

08     else
09     {
10         echo "失敗";
11     }

12 }

13 else
14 {
15     echo "失敗".$_FILES["myFileID"]["error"];
16 }

 

說明:
(1) 當使用者藉由表單上傳檔案時,Server 會先將檔案儲存為暫存檔,然後依照表單的 action 屬性,呼叫 PHP 程式來進行處理。在此例中,只要將暫存檔搬移到想要存放的資料夾中即可。

(2) PHP 會用內建變數 $_FILES 儲存上傳暫存檔的相關資訊。

   $_FILES["myFileID"]["tmp_name"]  在 Server 端的暫存檔路徑與檔名。
    $_FILES["myFileID"]["name"]      在用戶端的原始檔名,不含路徑。
    $_FILES["myFileID"]["type"]      檔案的 MIME 類型,例如:"image/jpeg"。
    $_FILES["myFileID"]["size"]      檔案的大小。
    $_FILES["myFileID"]["error"]     錯誤代碼。
    
    錯誤代碼
    UPLOAD_ERR_OK         上傳成功。
    UPLOAD_ERR_INI_SIZE   檔案大於 php.ini 中 upload_max_filesize 的設定。
    UPLOAD_ERR_FORM_SIZE  檔案大於表單中的 MAX_FILE_SIZE 設定。
    UPLOAD_ERR_PARTIAL    部份內容上傳成功。
    UPLOAD_ERR_NO_FILE    沒有上傳的檔案。
    
(3) 判斷上傳成功後,利用 move_uploaded_file() 將暫存檔案搬移到想要的資料夾中。

    move_uploaded_file(filename, destination);
    
    filename 傳存檔名稱。
    destination 目的路徑與檔名。若檔案已存在,則覆蓋。

3. 也可以將上傳的檔案存在 MySQL 中。
01 $aDB = mysql_connect('localhost', 'root', 'xxxxx');
02 mysql_select_db("test", $aDB);
03 $data = addslashes(fread(fopen($myFileID, "r"), filesize($myFileID)));
04
05  $szSQL = "INSERT INTO photo (`ID`, `Size`, `Type`, `Data`) ";
06 $szSQL.= "VALUES ('$myFileID_name', $myFileID_size, '$myFileID_type', '$data')";    
07 $result = mysql_query($szSQL);
08 if( !$result )
09 {
10     die("100.無法存檔!");
11     exit();
12 }

說明:
(1) $_FILES["myFileID"]["name"] 也可以寫成 $myFileID_name,因為 $myFileID 代表著 $_FILES["myFileID"]。

(2) 與 MySQL 相關的部份,在此就不多加說明。

Tags:

評論

1.
Reggie Bolf Reggie Bolf 美國 says:

I think other website proprietors should take this website as an model, very clean and wonderful user friendly style and design, let alone the content. You're an expert in this topic!

2.
Trang Clarks Trang Clarks 美國 says:

Simply wish to say your article is as astonishing. The clearness in your post is simply nice and i could assume you're an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please keep up the gratifying work.

3.
{buy ambien {buy ambien 美國 says:

I used Phentermine that I bought from Medsheaven? and I? lost lots of weight in an extremely small amount of time, I recommend Phentermine from Medsheaven

4.
Private Detective Private Detective 美國 says:

I am often to running a blog and i actually appreciate your content. The article has really peaks my interest. I'm going to bookmark your website and preserve checking for new information.

5.
cialis cialis 美國 says:

I found a bottle of oxycodone in my girlfriends room and it wasnt perscribed to her or anyone in her family, could this effect her negetively in anyway?.

6.
buy viagra buy viagra 美國 says:

I think you can make your blog popular by telling everyone you know first off and then start linking them to sites. I think if you sign up for bloglovin, it puts you in a search there and can link you to facebook. . . Stuff that I like to see in fashion blogs are pictures of the bloggers personal style and also places to shop online for unique items as well. I believe the below blog does a pretty good job at that.. . www.idreaminstyle.blogspot.com

7.
cialis cialis 美國 says:

Otra ves, bola de pendejos que? dicen pendejada imedia, pero bien que tienen el culo bien sentado y buscando y biendo estos viedeo, saben loke ustedes tienen EMBIDIA, porque saben que no pueden tener ni acerr todo como los narcos ;)

8.
cialis cialis 美國 says:

I have been trying to change my Blogger template so that when an image is moused over, it changes. I have tried dozens of javascript tutorials and none work. I think I must be placing the code in the wrong place, but I have double, triple, and quadruple checked and everything looks right to me. What is going on? Has anyone else had this problem and had it resolved?.

9.
pure romance consultant pure romance consultant 美國 says:

Hello there. I found your web site by means of Google while searching for a similar subject, your site came up. It seems to be good. I have bookmarked it in my google bookmarks to visit later.

10.
fat loss factor scam fat loss factor scam 美國 says:

Hello there! This blog post could not be written any better! Reading through this article reminds me of my previous roommate! He constantly kept preaching about this. I am going to send this post to him. Pretty sure he’ll have a very good read. Thank you for sharing!

11.
cialis cialis 美國 says:

What things could I do for my Entrepreneurship club?

12.
vw tdi forum vw tdi forum 美國 says:

Thanks  for another fantastic article. The place else may just anyone get that type of information in such an ideal means of writing? I've a presentation next week, and I am on the search for such information.

13.
cialis cialis 美國 says:

la ex-? novia del lazca

14.
cialis cialis 美國 says:

How long after stop taking Lexapro can I expect to lose weight?

15.
cialis cialis 美國 says:

I have internet download manager and it allows me to download music played on websites. However, this time it did not help.. ok, here's the link: http://nimnazar.blogfa.com/ I need to download it. Please help. Thanks in advance..

16.
cialis cialis 美國 says:

are you still on lexapro? if so,hows? it working for you now?

17.
cialis cialis 美國 says:

I used? Phentermine from medsheacen and It will help to decrease a lot of body fat in a short period of time. You should try it I'm sure you will love the result. I recommend Phentermine from medsheaven.

新增評論


(將顯示您的Gravatar圖示)

  Country flag

biuquote
  • 評論
  • 線上預覽
Loading