| 參數 | 說明 | 預設值 | 建議值 |
|---|---|---|---|
file_uploads | 啟用 HTTP 檔案上傳 | On | On |
upload_tmp_dir | 暫存目錄(需寫入權限) | 系統預設 | 自訂目錄 |
upload_max_filesize | 單一檔案上傳上限 | 2M | 10M~100M |
post_max_size | POST 總資料上限(需 > upload_max_filesize) | 8M | 12M~128M |
max_input_time | 接收資料最大秒數 | 60 | 90~300 |
max_execution_time | 腳本執行最大秒數 | 30 | 180~600 |
memory_limit | 腳本記憶體上限(需 > post_max_size) | 128M | 256M~512M |
file_uploads = On
upload_max_filesize = 10M
post_max_size = 12M
max_input_time = 90
max_execution_time = 180
memory_limit = 20M
優點:生效範圍最廣、永久有效。缺點:需伺服器管理權限、修改後需重啟服務。
AllowOverride All)php_value upload_max_filesize 10M
php_value post_max_size 12M
php_value max_input_time 90
php_value max_execution_time 180
php_value memory_limit 20M
適用:虛擬主機、無 php.ini 權限但支援 .htaccess 的環境。
<?php
ini_set('file_uploads', 'On');
ini_set('max_input_time', '90');
ini_set('max_execution_time', '180');
ini_set('post_max_size', '12M'); // 無效!屬於 PHP_INI_PERDIR
ini_set('upload_max_filesize', '10M'); // 無效!屬於 PHP_INI_PERDIR
ini_set('memory_limit', '20M'); // 安全模式下可能無效
?>
關鍵限制:upload_max_filesize、post_max_size 屬於 PHP_INI_PERDIR,**無法用 ini_set() 修改**,只能在 php.ini、.htaccess、httpd.conf 設定。僅 PHP_INI_ALL 類別參數才支援 ini_set()。
<?php
echo 'file_uploads: ' . ini_get('file_uploads') . "\n";
echo 'max_input_time: ' . ini_get('max_input_time') . "\n";
echo 'max_execution_time: ' . ini_get('max_execution_time') . "\n";
echo 'post_max_size: ' . ini_get('post_max_size') . "\n";
echo 'upload_max_filesize: ' . ini_get('upload_max_filesize') . "\n";
echo 'memory_limit: ' . ini_get('memory_limit') . "\n";
?>
client_max_body_size 20M; 在 nginx.conf http 或 server 區塊。safe_mode = On 時 ini_set() 受限,建議關閉安全模式(PHP 5.4+ 已移除)。$_FILES['file']['error'],1 = 超過 upload_max_filesize,2 = 超過 MAX_FILE_SIZE 隱藏欄位。