Jump to content

Looping Through Image Directory


neridaj

Recommended Posts

Hello,

 

I have a user registration page that builds a directory named "username" upon successful registration. This directory is then populated with photos for the user to download upon successful login. I would like to know how I can loop through this directory to build and populate a table with all the photos inside of it by grabbing any .jpg file type. I'm assuming I would use some regular expression to grab the .jpg files, but I'm not sure how to loop through a directory to accomplish this. If anyone can help me with this I would really appreciate it.

 

Thanks,

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/90464-looping-through-image-directory/
Share on other sites

<?php
$dir = dir('username');
while(($file = $dir->read()) !== false) {
  if(preg_match('^([%\w-]+\.(?:jpe?g¦JPE?G¦gif¦GIF¦png¦PNG)¦)$', $file)) {
   echo 'Image found!';
  }
}
$dir->close();
?> 

 

I suck at regular expressions so don't count on it!

I tried that and got this:

 

Fatal error: Call to a member function read() on a non-object

 

So I then tried this and an array listing the only the "." and ".." from it, without the file names:

 

$dir    = 'members/' . $_SESSION['valid_user'];

$files1 = scandir($dir);

$files2 = scandir($dir, 1);

 

print_r($files1);

print_r($files2);

 

result: Array ( [0] => . [1] => .. ) Array ( [0] => .. [1] => . )

 

Thanks for the help,

 

Jason

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.