1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to hide this error message "failed to open dir: "?

Discussion in 'PHP' started by ipunkbali, Jan 19, 2016.

  1. #1
    <?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
     
    ipunkbali, Jan 19, 2016 IP
  2. KangBroke

    KangBroke Notable Member

    Messages:
    1,026
    Likes Received:
    59
    Best Answers:
    4
    Trophy Points:
    265
    #2
    You are getting the error because the code is looking for a folder named /folder/ in your root. Why not just create the folder?
     
    KangBroke, Jan 19, 2016 IP
  3. ipunkbali

    ipunkbali Well-Known Member

    Messages:
    276
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    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.
     
    ipunkbali, Jan 19, 2016 IP
  4. Alexandru A.

    Alexandru A. Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    <?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.
     
    Alexandru A., Jan 19, 2016 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    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
     
    deathshadow, Jan 21, 2016 IP