七月 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.
buy viagra buy viagra 美國 says:

Great video guy. I hope you are still doing good. I took klonopin for many years, became tolerant, and almost died trying to detox. Don't ever cold turkey benzos like xanax or klonopin. Google Professor? Heather Ashton and follow the Ashton protocol. Benzo detox must be approached with extreme caution.

2.
Viagra Viagra 美國 says:

I completed the full course of Accutane prescribed to me by my dermatologist.  Accutane takes a different amount of time to work for everyone, but it's usually complete in about six months.  The important thing to remember is to not stop taking it until your doctor tells you to, even if you think it's all better, because the acne could come back if not fully treated.  Also, acne may get worse at the beginning of treatment.  Don't be alarmed and keep taking the medicine as prescribed; this is a common effect and will go away.

3.
deal in Miami deal in Miami 美國 says:

Hey. Very cool site!! Guy .. Beautiful .. Amazing .. I'll bookmark your web site and take the feeds also...I am satisfied to locate a lot of useful info here in the post. Thank you for sharing..

4.
bus tours to new york from moncton bus tours to new york from moncton 美國 says:

Hello, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me crazy so any help is very much appreciated.

5.
viagra viagra 美國 says:

Do I need to stop or continue using Lipitor?

6.
pozycjonowanie stron w google pozycjonowanie stron w google 美國 says:

Sorry for the huge review, but I'm really loving the new Zune, and hope this, as well as the excellent reviews some other people have written, will help you decide if it's the right choice for you.

7.
valium valium 美國 says:

Hircine got butthurt that Korgal's working for Meridia.

8.
photography sales training photography sales training 美國 says:

I'm so happy to read this. This is the type of info that needs to be given and not the accidental misinformation that's at the other blogs. Appreciate your sharing this greatest doc.

9.
Press Release Submission Press Release Submission 美國 says:

Hi, Neat post. There is a problem with your website in internet explorer, would check this… IE still is the market leader and a good portion of people will miss your fantastic writing because of this problem.

新增評論


(將顯示您的Gravatar圖示)

  Country flag


  • 評論
  • 線上預覽
Loading