PHP-MySQL系列 文件上傳
這篇文章主要在介紹基本的上傳文件
加入一些檢核的判斷句
可以防止使用者亂傳東西上來
文件相關基本函數
開啟文件fopen()
關閉文件fclose()
讀取單行fgets()
讀取單字符fgetc
檢查文件結尾feof()
想學更細的:https://www.w3schools.com/php/php\_file\_open.asp
文件上傳前置作業
- 要先開啟php.ini中的file_uploads
 input標籤的
- type必須為file
- method要是post
- enctype要是multipart/form-data- enctype屬性指定將表單內容提交到Server時應如何編碼
 
- 全域變數 - $_FILES
 假設input了一個name=’userfile’的欄位
 則- $_FILES陣列中包含了下面幾個元素:- 1 
 2
 3
 4
 5- $_FILES['userfile']['name'] //local端的檔案原名稱。 
 $_FILES['userfile']['type'] //檔案的 MIME 型別,如果瀏覽器提供此資訊的話。
 $_FILES['userfile']['size'] //已上傳檔案的大小,單位為位元組。
 $_FILES['userfile']['tmp_name'] //檔案被上傳後在伺服器端儲存的臨時檔名。
 $_FILES['userfile']['error'] //和該檔案上傳相關的錯誤程式碼。
- 會用到的其他函數 - is_uploaded_file: 判斷檔案是否是通過 HTTP POST 上傳的- move_uploaded_file(string $filename, string $destination): 將上傳的檔案移動到新位置
- 先建立像下面這樣子的目錄結構 - file__upload/file_upload/裡面會儲存使用者上傳內容 
文件上傳範例
| 1 | 
 | 
| 1 | 
 | 
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
 Comment
GitalkUtterances
