Editing
Troubleshooting of PHP errors
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Configuration and Environment Issues == === 不知道網站伺服器載入哪一個 php.ini 設定檔 === 使用 console command<ref>[http://php.net/manual/en/features.commandline.options.php PHP: Options - Manual]</ref> * 輸入命令 {{kbd | key = <nowiki>php --ini</nowiki>}} 或 {{kbd | key = <nowiki>/path/to/php --ini</nowiki>}} <pre> > php --ini Configuration File (php.ini) Path: C:\Windows Loaded Configuration File: C:\xampp\php\php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none) </pre> * 使用 [http://php.net/manual/en/function.phpinfo.php phpinfo] 確認「Configuration File」<ref>[http://www.commandlinefu.com/commands/view/818/phpinfo-from-the-command-line phpinfo from the command line | commandlinefu.com]</ref>: {{kbd | key = <nowiki>echo "<?php phpinfo(); ?>" | php | grep "Configuration File"</nowiki>}} ** 指定完整 php 路徑 command: *** {{kbd | key = <nowiki>/Applications/XAMPP/bin/php -r 'echo phpinfo();' | grep "Configuration File"</nowiki>}} *** 或 {{kbd | key = <nowiki>echo "<?php phpinfo(); ?>" | /Applications/XAMPP/bin/php | grep "Configuration File"</nowiki>}} * {{kbd | key = <nowiki>php -i | grep "Configuration File"</nowiki>}} === 執行PHP時顯示原始碼的錯誤 === 確認伺服器是否能執行 PHP * 驗證方式: ** 網頁伺服器放置寫了 [http://php.net/manual/en/function.phpinfo.php phpinfo] 的 PHP 檔案,看是否可以顯示結果。使用後建議移除該檔案,避免對外揭露伺服器環境資訊。或 ** {{kbd | key = <nowiki>php -v</nowiki>}} 如果可以顯示 PHP 版本資訊,代表有安裝 PHP 成功。 * 解決方式: ** 不能執行 PHP 的話,需要檢查 [http://php.net/manual/en/install.php 安裝 PHP] 步驟。 如果 PHP 使用 short tag 語法撰寫,但是沒有啟用 short_open_tag ,會造成顯示原始碼的錯誤。 * 驗證方式: ** 輸入指令 {{kbd | key = <nowiki>php -i | grep short_open_tag</nowiki>}} 確認結果為 short_open_tag = Off * 解決方式: ** 法1: 啟用 short_open_tag (1) php.ini 檔案 需要開啟 short_open_tag = Off -> On (2) 重新啟動網頁伺服器服務,讓 php.ini 修改生效 (3) 驗證 short_open_tag = On ** 法2: 不啟用 short_open_tag,將多個 PHP 檔案的 {{kbd | key=<nowiki><? </nowiki>}} 改成 {{kbd | key=<nowiki><?php </nowiki>}}。 (1) 使用支援 [[Regular expression]] 的文字編輯軟體、(2) 搜尋 {{kbd | key=<nowiki><\?\s</nowiki>}} 取代為 {{kbd | key=<nowiki><\?php </nowiki>}} 。請注意取代部分結尾有一個空白。詳見 [[Batch remove PHP short tags]] === curl error 7 while downloading https://packagist.tw/ ... === Error condition <pre> composer.phar update Loading composer repositories with package information https://packagist.tw could not be fully loaded (curl error 7 while downloading https://packagist.tw/packages.json: Failed to connect to packagist.tw port 443: Connection refused), package information was loaded from the local cache and may be out of date In CurlDownloader.php line 372: curl error 7 while downloading https://packagist.tw/p/zircote/xxx.json: Failed to connect to packagist.tw port 443: Connection refused </pre> Solution * The mirror I was using encountered a network error. * Switch to a different mirror by entering the following command: {{kdb | key=<nowiki>composer config repos.packagist composer https://packagist.org</nowiki>}} === 錯誤訊息: phpinfo() has been disabled === <pre> PHP Warning: phpinfo() has been disabled for security reasons in /path/to/phpinfo.php on line xx </pre> * 原因: 網管關閉使用 phpinfo 替代解決方法: 使用 console command * {{kbd | key = <nowiki>php -i | grep -i "變數名稱"</nowiki>}} 或 {{kbd | key = <nowiki>/path/to/bin/php -i | grep -i "變數名稱"</nowiki>}} for {{linux}} ** {{kbd | key = <nowiki>php -i</nowiki>}} 變數 --info 、 -i 或 -ini,用途顯示PHP資訊或設定值。(PHP information and configuration 摘錄自{{kbd | key = <nowiki>man php</nowiki>}}) ** | 管線命令 (pipe),詳 [http://linux.vbird.org/linux_basic/0320bash.php#pipe 鳥哥的 Linux 私房菜 -- 學習 bash shell] ** {{kbd | key = <nowiki>grep -i</nowiki>}} 變數 -i 代表忽略搜尋關鍵字或搜尋條件 (pattern) 的大小寫 * {{kbd | key = <nowiki>/path/to/bin/php -m</nowiki>}} 列出載入的模組名稱 (無版本資訊)<ref>[http://php.net/manual/en/function.extension-loaded.php PHP: extension_loaded - Manual]</ref> === 錯誤訊息: Allowed memory size of XXX bytes exhausted (out of memory) === * 訊息: PHP Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XX bytes) <ref>[https://www.airpair.com/php/fatal-error-allowed-memory-size Fixing PHP Fatal Error: Allowed Memory Size Exhausted]</ref> * 可能原因: ** 一次讀取13萬行的資料,發生記憶體錯誤、 ** 無窮迴圈 (infinite loop) * 解決方法: ** 如果使用 32 位元 (x86) 的 PHP,改成使用 64 位元 (x64) 的 PHP ** 增加 memory_limit<ref>[http://blog.xuite.net/chingwei/blog/30054402-%E3%80%90%E7%B3%BB%E7%B5%B1%E3%80%91PHP+-+Fatal+error%3A+Allowed+memory+size+of+xxx+bytes+exhausted 【系統】PHP - Fatal error: Allowed memory size of xxx bytes exhausted @ My Life :: 隨意窩 Xuite日誌] </ref> ** 跟檔案處理有關: *** (1) 減少每次讀取的資料行數,例如每次只讀取 50 行的資料筆數來做處理。 *** (2) 如果檔案較大,應避免使用一次讀取全部檔案內容的 [http://php.net/manual/en/function.file-get-contents.php file_get_contents]、[http://php.net/manual/en/function.file-put-contents.php file_put_contents] 等函數,讀取檔案建議改成使用 [https://www.php.net/manual/en/language.generators.overview.php Generators] 或 [http://php.net/manual/en/function.fgets.php fgets]、而寫入檔案則可使用 [https://www.php.net/manual/en/function.fwrite.php fwrite] append 方式寫入<ref>[https://davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete Basic PHP File Handling -- Create, Open, Read, Write, Append, Close, and Delete]</ref>。 *** (3) 如果透過 MySQL 執行匯出資料表的 CSV 檔案,則可以不要選擇欄位名稱,而是選擇全部欄位直接匯出。原因:「Rather than attempting to build the object-tree, you could directly try to select the result into a file」<ref>[https://stackoverflow.com/questions/31471186/how-to-export-millions-of-rows-from-mysql-to-csv-via-php-without-exhausting-memo How to export millions of rows from MySQL to CSV via PHP without exhausting memory? - Stack Overflow]</ref> *** (4) 如果跟 json 檔案處理有關,可以搭配使用 [https://stedolan.github.io/jq/ jq] 可以有效處理大檔案的 json 檔案 ** [http://php.net/manual/en/control-structures.foreach.php foreach] 時,與其寫入整個陣列到記憶體,可以改用 [http://php.net/manual/en/language.generators.overview.php Generators] 寫法,節省記憶體的使用。 ** [https://errerrors.blogspot.com/2021/03/allowed-memory-exhausted.html 解決網站伺服器遇到 Allowed memory exhausted 問題] === 錯誤訊息: It is not safe to rely on the system's timezone settings === * 訊息: Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. * 原因: 沒有設定時區 * 解決方法: 使用 date() 或 strtotime() 等[http://php.net/manual/en/ref.datetime.php 時間相關函數]之前,先設定時區,例 {{kbd | key = <nowiki>date_default_timezone_set("Asia/Taipei")</nowiki>}} === composer.phar: command not found 或 Could not open input file: composer.phar === 錯誤訊息: 透過 [https://getcomposer.org/ composer] 安裝 PHP 套件時,例如 {{kbd | key = composer require league/csv}},出現錯誤訊息「composer.phar: command not found」或「Could not open input file: composer.phar」 * 解法I: 如果沒有設定 composer.phar 的 PATH ,需要清楚告知 composer.phar 的完整檔案路徑,例如{{kbd | key = /path/to/php /path/to/composer require league/csv}} * 解法II on {{Mac}}/{{Linux}}: 將下載的 composer.phar 移動到 /usr/local/bin/composer {{kbd | key = mv /path/to/composer.phar /usr/local/bin/composer}},再重新執行安裝套件的指令<ref>[http://stackoverflow.com/questions/21670709/running-composer-returns-could-not-open-input-file-composer-phar php - Running Composer returns: "Could not open input file: composer.phar" - Stack Overflow]</ref>。
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information