> For the complete documentation index, see [llms.txt](https://virusking.gitbook.io/php/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://virusking.gitbook.io/php/10.-yong-php-shang-an.md).

# 9. 用PHP上傳檔案

1. 若表單中有file元件，表單一定要加上：「enctype="multipart/form-data"」。
2. 每上傳一張圖（假設file欄位名稱為pic），都會產生一組 $\_FILES 超級全域變數：
   * (1) $\_FILES\['pic']\['name']（多檔：$\_FILES\['pic']\['name']\[0]）：上傳檔案原始名稱。
   * (2) $\_FILES\['pic']\['type']：檔案的 MIME 類型，例如“image/gif”。
   * (3) $\_FILES\['pic']\['size']：已上傳檔案的大小，單位為bytes。
   * (4) $\_FILES\['pic']\['tmp\_name']：檔案被上傳後的臨時檔案名。
   * (5) $\_FILES\['pic']\['error']：和該檔案上傳相關的錯誤代碼。
3. 上傳的步驟：送出上傳→圖會暫時放到tmp中→程式要搬移該檔到指定的位置。
4. 搬移上傳檔方法：[move\_uploaded\_file(暫存檔 , 新路徑檔名)](http://www.php.net/move_uploaded_file)

&#x20;讀出檔案會用到的函數：

1. opendir()：<http://www.php.net/opendir>
2. preg\_match()：<http://www.php.net/preg_match>
