PHP7 下檔案名稱太長導致 glob 回傳 false 的問題
很多時候在開發 PHP 的時候我們都會使用這個超級方便的 glob(*) 功能。簡單來說就是把一個資料夾或路徑內的檔案以 array 方式回傳的方法。然而,在最近的 ArOZ Online 測試伺服器更新之後發現了 File Explorer 在開啟某些資料夾時出現 PHP ERROR 的問題。後來經過一堆檢測之後發現原來是檔案名稱太長了,結果 glob 就直接回傳 false 。 原本的 listdir.php //Some code here for auth and var parsing if (file_exists($path) && is_dir($path)){ $filelist = glob($path . "/*"); //Other code } 要解決這個問題,我們需要把它改成使用 scandir ,然而由於 scandir 會多了很多代碼,為了節省處理能力,我們只在需要的時候使用 scandir 就好。結果新的 glob 部分就變成了這樣: //Some code here for auth and var parsing if (file_exists($path) && is_dir($path)){ $filelist = glob($path . "/*"); if ($filelist === false){ //Something went wrong while trying to list the directory using glob. Try scandir $scanResults = scandir($path); $filelist = []; $useAbnormalFilter = true; foreach ($scanResults as $fileObj){ if ($fileObj != "." && $fileObj != ".."){ array_push($filelist, $path . "/" . $fileObj); } } } //Other code } 就是這樣,File Explorer 的顯示就回復正常了。對,這都是 Windows 的錯。