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
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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.