PHP 檔案上傳下載
1. php.ini ?配置 ? ?
?
;;;;;;;;;;;;;;;;;;;; Resource Limits ; 資源限制;;;;;;;;;;;;;;;;;;;; Maximum execution time of each script, in seconds; http://php.net/max-execution-time; Note: This directive is hardcoded to 0 for the CLI SAPImax_execution_time = 30 ; Maximum amount of time each script may spend parsing request data. It's a good; idea to limit this time on productions servers in order to eliminate unexpectedly; long running scripts. ; Note: This directive is hardcoded to -1 for the CLI SAPI; Default Value: -1 (Unlimited); Development Value: 60 (60 seconds); Production Value: 60 (60 seconds); http://php.net/max-input-timemax_input_time = 60; Maximum input variable nesting level; http://php.net/max-input-nesting-level;max_input_nesting_level = 64; Maximum amount of memory a script may consume (128MB); http://php.net/memory-limitmemory_limit = 128M
???max_execution_time每個指令碼頁面完成執行操作的最大時間,單位是秒。如果為-1,則沒有限制
?? max_input_time每個指令碼頁面處理請求資料的最大時間,單位是秒。如果為-1,則沒有限制
?? memory_limit一個指令碼頁面所能消耗的最大記憶體
?
; Maximum size of POST data that PHP will accept.; http://php.net/post-max-sizepost_max_size = 8M
?? ?PHP通過表單POST所能接收的最大值,包括表單裡的所有項。
?
;;;;;;;;;;;;;;;;; File Uploads ;;;;;;;;;;;;;;;;;; Whether to allow HTTP file uploads.; http://php.net/file-uploadsfile_uploads = On; Temporary directory for HTTP uploaded files (will use system default if not; specified).; http://php.net/upload-tmp-dirupload_tmp_dir = "e:/wamp/tmp"; Maximum allowed size for uploaded files.; http://php.net/upload-max-filesizeupload_max_filesize = 2M
?? ?file_uploads是否允許HTTP上傳,預設為on,即為開啟,無需修改
?? ?upload_tmp_dir檔案上傳時的臨時儲存目錄。如果沒有指定,會用系統預設的臨時檔案夾
?? ?upload_max_filesize允許上傳的檔案的最大值
?
2. 版面設定 ? ?3個條件缺一不可
(1)type屬性設定為file
(2)form的enctype="multipart/form-data"
(3)method="post"
?
?
?
???
?? 建議在上傳表單中增加一個hidden隱藏欄位,允許上傳的最大位元組數。當檔案上傳大於1MB時,$FILES中的error值將返回2
?
3. bool move_uploaded_file(string filename,string destination) ?函數是PHP中專門用來上傳檔案的。
?? ?filename用戶端源檔案的檔案名稱及其路徑,上傳檔案的臨時檔案名稱,即$_FILES[tmp_name]
?? ?destination上傳後儲存在伺服器的新的路徑和名稱
?? ?move_uploaded_file() 作用只是將檔案換個地方儲存,這是出於安全的考慮
?? ?bool is_uploaded_file(string filename) ?判斷檔案是否為POST上傳
?
?? $_FILES[]['name']: ? ? ? ? ? 用戶端源檔案名稱
?? $_FILES[]['type']: ? ? ? ? ? ? 上傳檔案的類型
?? $_FILES[]['size']: ? ? ? ? ? ? ?上傳檔案的位元組數
?? $_FILES[]['tmp_name']: ? ?上傳後在伺服器上的臨時隱藏檔名
?? $_FILES[]['error']: ? ? ? ? ? ? 上傳檔案的錯誤碼
?
4. ?實現下載 ?實現下載最常用的方法通過連結下載和通過header()函數下載。
?
echo "".$filen."
";
?
"" '' .三者如何配合使用?
?
?
?
?
?
?