I am trying to iterate through a directory and display all the pictures inside. I got it to work until I added Jquery/ajax into the equation.. Somehow the path is getting duplicated along the way so I get this error (you can see where it is duplicated):
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'DirectoryIterator::__construct(images/gallery/album1,images/gallery/album1) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: The system cannot find the path specified
I can not for the life of me figure out why it is coming out like that…contruct(images/gallery/album1,images/gallery/album1) should be construct(images/gallery/album1)
PHP:
$album = $_POST['album']; $dir = new DirectoryIterator("images/gallery/$album"); foreach ($dir as $fileInfo) { if($fileInfo->isDot()) continue; $pic = $fileInfo->getFilename(); print "<div> <img src='images/gallery/$album/$pic'> </div>"; }
Jquery/ajax:
function albumChosen(id) { var id = id; var album = $('a[id="'+id+'"]').attr("rel"); $.ajax({ url: "PHPscripts/getAlbums.php", type: "POST", data: {'album' : album}, success: function(data){ $('#galleryList').html(data); } }); return false;}
If I alert(album); it displays ‘album1’ correctly. So it has something to do with how its reading it from the $_POST
EDIT:
I added the code suggested..
$album = $_POST['album'];
print_r($_POST);
var_dump($album);
$dir = new DirectoryIterator("images/gallery/$album");
//rest of code...
print_r($_POST) prints out Array ( [album] => album1 )
var_dump($album) prints out string 'album1' (length=6)
Turns out I just needed to add
../to the front of my path.. If it didn’t duplicate the path the way it did I may have figured this out sooner, but because of that I didn’t think there was anything wrong with the path. So here is the complete code that works, again all I added was the../in front of the path.