Question

Reading up on scandir(), having an issue getting anything to display.

<?php
    $dir = 'http://www.universaldynamicmedia.com/sandbox/Images';
    $array = scandir($dir);
    foreach($array as $key => $value)
        {
            echo '<li><img src="' . $dir . $value . '" /></li>';
        }
?>

I supposed I'm not doing something right with the directory and scandir() as the other stuff, I've done before and it works fine.

Would directory permissions cause an issue?

Was it helpful?

Solution

** Note you cannot use a URL, you must have access to the folders/files

scandir():

$dir = "PATH_TO_DIRECTORY";
$exclude = array( ".","..","error_log","_notes" );
if (is_dir($dir)) {
    $files = scandir($dir);
    foreach($files as $file){
        if(!in_array($file,$exclude)){
            echo '<li><img src="' . $dir . $file . '" /></li>';
        }
    }
}

readdir():

$dir = "PATH_TO_DIRECTORY";
$exclude = array( ".","..","error_log","_notes" );
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(!in_array($file,$exclude))
                 echo '<li><img src="' . $dir . $file . '" /></li>';
        }
        closedir($dh);
    }
}?>

OTHER TIPS

scandir() only works on local filesystems, you need to change that to:

$dir = '/path/to/document/root/sandbox/Images';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top