實戰中常遇到的兩個由 php.ini 配置不當引發的報錯:AJAX 圖片上傳「無法創建臨時文件」與 exec() 壓縮資源失效。本文提供根因分析與 Windows/Linux 雙平台修復方案。
File upload error - unable to create a temporary fileupload_tmp_dir 未設置或指向的目錄無寫入權限,PHP 退回系統默認臨時目錄(如 C:\Windows\Temp 或 /tmp),但進程用戶(IIS_IUSRS / www-data / nginx)無權限讀寫。
php.ini:upload_tmp_dir = "D:/php/tmp" (Windows)upload_tmp_dir = "/var/www/php_tmp" (Linux)IIS_IUSRS / IUSR → 勾選「讀取/寫入/修改」。mkdir -p /var/www/php_tmp && chown www-data:www-data /var/www/php_tmp && chmod 755 /var/www/php_tmpexec() / shell_exec() 被禁用導致前端資源壓縮失敗代碼邏輯、權限均正常,但調用 exec('uglifyjs ...') 無輸出、無報錯,實際被 disable_functions 截斷。
disable_functions,移除 exec、shell_exec、passthru、proc_open 等:disable_functions = "pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,"open_basedir 限制執行路徑。C:\Windows\System32\cmd.exe → 右鍵屬性 → 安全 → 編輯 → 添加 IIS_IUSRS / IUSR → 勾選「讀取與執行」「讀取」。/usr/bin/uglifyjs)的執行權限即可。php -r "exec('echo test', \$o); var_dump(\$o);" 應輸出 array(1) { [0]=> string(4) "test" }。exec 系函數是基線安全要求,若業務必須調用外部命令,優先考慮:imagick 替代 convert、ZipArchive 替代 zip)。open_basedir 限制可讀寫目錄,結合 disable_functions 最小化攻擊面。