|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-09-07 23:13 UTC] worthbob01 at yahoo dot com
Description: ------------ recursive scandir causes seg fault. code example from http://us2.php.net/scandir User Contributed Notes. Reproduce code: --------------- <?php $afiles = scandir_recursive("/tmp"); function scandir_recursive($directory) { $folderContents = array(); $directory = realpath($directory).DIRECTORY_SEPARATOR; foreach (scandir($directory) as $folderItem) { if ($folderItem != "." AND $folderItem != "..") { if (is_dir($directory.$folderItem.DIRECTORY_SEPARATOR)) { $folderContents[$folderItem] = scandir_recursive( $directory.$folderItem."\\"); } else { $folderContents[] = $folderItem; } } } return $folderContents; } ?> Expected result: ---------------- nothing as there is not output in the sample. just expected it to run without a seg fault. Actual result: -------------- [bob@dev design]# date;php sample.php;date Wed Sep 7 16:11:11 CDT 2005 Segmentation fault Wed Sep 7 16:11:12 CDT 2005 [bob@dev design]# PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 08:00:02 2025 UTC |
Your function is stuck in a recursive loop, eventually this will seg fault. If you wanna see it in action add the following lines to the top of the function. static $depth = 0; echo $depth++; To fix the problem remove the . "\\" from the tail of "scandir_recursive($directory.$folderItem."\\");". Here's a cleaned up function to not display errors and so forth. function scandir_recursive($directory) { $folderContents = array(); $directory = realpath($directory) . DIRECTORY_SEPARATOR; $folderItems = @scandir($directory); if (!is_array($folderItems)) return "No access"; foreach ($folderItems as $folderItem) { if (substr($folderItem, 0, 1) != ".") { // Ignore anything hidden if (is_dir($directory . $folderItem . DIRECTORY_SEPARATOR)) $folderContents[$folderItem] = scandir_recursive($directory . $folderItem); else $folderContents[] = $folderItem; } } return $folderContents; } ^_^