<?php $h = opendir("/home/public_html/folder/"); while (false !== ($entry = readdir($h))) { if($entry != '.' && $entry != '..') { //Skips over . and .. break; //Exit the loop so no more files are read } } ?> PHP: I keep getting this: "Warning: opendir(/home/public_html/folder/): failed to open dir: No such file or directory in ..." How to hide this error message when some folders dont exist? Thanks
You are getting the error because the code is looking for a folder named /folder/ in your root. Why not just create the folder?
Already have thousands folders, but sometimes users looking for folders that dont exist, and that cause that error. im thinking about using if(is_dir, but dont know how to apply it to that code.
<?php $filename = '/home/public_html/folder/'; if(file_exists($filename)) { $h = opendir("/home/public_html/folder/"); echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } while (false !== ($entry = readdir($h))) { if($entry != '.' && $entry != '..') { //Skips over . and .. break; //Exit the loop so no more files are read } } ?> PHP: Try something along those lines perhaps. "file_exists" seems to be a good way to check if a file or folder exists before attempting to do anything with said file or folder.
One of the reasons I prefer glob... though your loop/break doesn't make any blasted sense... if ($files = glob('/home/public_html/folder/*')) { foreach ($files as $fileName) { if (($fileName != '.') && ($fileName != '..')) { // process $fileName here } } } else { // error handler here } Code (markup): Also nice is that glob lets you pass it wildcards, and flags like GLOB_MARK can make it really easy to isolate things like directories by their first character. http://php.net/manual/en/function.glob.php