15,013
edits
No edit summary |
mNo edit summary |
||
| Line 55: | Line 55: | ||
* Switch to a different mirror by entering the following command: {{kdb | key=<nowiki>composer config repos.packagist composer https://packagist.org</nowiki>}} | * 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>}} | |||
=== 移除非預期的空白(全形空白) === | |||
需要額外移除 [http://www.fileformat.info/info/unicode/char/3000/index.htm 'IDEOGRAPHIC SPACE' (U+3000)] 全形空白 | |||
<pre> | |||
$string = str_replace(json_decode('"\u3000"'), "", $string); | |||
$string = trim($string); | |||
</pre> | |||
More on: [[Data_cleaning#Remove other string look like whitespace | Remove other string look like whitespace]] | |||
== | |||
=== 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>。 | |||
== File and Directory Issues == | |||
=== 檔案操作有關 === | === 檔案操作有關 === | ||
| Line 78: | Line 128: | ||
*# PHP [https://www.php.net/manual/en/function.get-current-user.php get_current_user] function | *# PHP [https://www.php.net/manual/en/function.get-current-user.php get_current_user] function | ||
*# PHP [http://php.net/manual/en/function.is-writable.php is_writable function] | *# PHP [http://php.net/manual/en/function.is-writable.php is_writable function] | ||
=== 錯誤訊息: no such file or directory 找不到 PHP 路徑 === | === 錯誤訊息: no such file or directory 找不到 PHP 路徑 === | ||
| Line 119: | Line 151: | ||
</pre> | </pre> | ||
And then | And then relaunch the shell | ||
=== PHPExcel 產生的 Excel 檔案,開啟後看到一堆亂碼 === | |||
狀況1: 開啟 Excel 檔案,看到最前面有 {{kbd | key=<nowiki>NOTICE</nowiki>}} 的錯誤訊息,例如 {{kbd | key=<nowiki><b>Notice</b>: Constant CONST_XXX already defined in <b>path\to\script.php</b> on line xx</nowiki>}} | |||
* 解法: 關閉錯誤訊息輸出 ex: [http://www.php.net/manual/en/function.error-reporting.php error_reporting(0);] 以及修正 NOTICE 訊息指涉的問題,例如例子中常數重複宣告的問題。 | |||
狀況2: 開啟 Excel 檔案,看到最前面有 {{kbd | key=<nowiki>Fatal error</nowiki>}} 之類的錯誤訊息 | |||
* 解法: 關閉錯誤訊息輸出 ex: [http://www.php.net/manual/en/function.error-reporting.php error_reporting(0);] ,以及修正 Fatal error 訊息指涉的問題。 | |||
狀況3: 開啟 Excel 檔案,看到有儲存格位址之類的錯誤訊息 | |||
* 解法: 該儲存格內容以等號 (=) 開始,卻不是函數。解決方式是將儲存格內容的最前面加一個單引號 (') 或者是加個空白。 | |||
== Unclassified PHP 技術問題處理 == | |||
=== 錯誤訊息: ERROR: 00000:: === | |||
* 原因: 想要刪除的資料不在 MySQL 資料庫內,進行刪除時,會發生的錯誤。 | |||
* 解決方法: 先檢查該資料是否存在,再進行刪除。 | |||
=== 錯誤訊息: PHP syntax error “unexpected $end” === | === 錯誤訊息: PHP syntax error “unexpected $end” === | ||
| Line 163: | Line 214: | ||
相關頁面: [[Email testing]] | 相關頁面: [[Email testing]] | ||
=== COMPOSER 升級 PHPUNIT 版本 6 到 7 遇到問題 YOUR REQUIREMENTS COULD NOT BE RESOLVED TO AN INSTALLABLE SET OF PACKAGES === | === COMPOSER 升級 PHPUNIT 版本 6 到 7 遇到問題 YOUR REQUIREMENTS COULD NOT BE RESOLVED TO AN INSTALLABLE SET OF PACKAGES === | ||