Jump to content

[SOLVED] Read files in a directory


djpic

Recommended Posts

Ok, here is what I want to do.  I want to be able to read the contents of a directory with php.  Here is exactly what I am AIMing for:

 

Upload (FTP) a group of images to a directory on the server.  Then, create a php code that will go through that directory giving me the full directory location (i.e. /home/test/photos/imagefile.jpg) for each file.  That is the first task.

 

The second task is for the program to find any subdirectories in that file.  Once the program is finished reading the file names from the main directory, it goes to each of the sub-directory and finds all the files in those directories.

 

My goal is to upload a group of pictures and then have a loop tag going through each of the photos, adding the information to a MySQL database, compress them, and copy them to a different folder.  Please note though, I know how to do everything but getting the program to read the files in the directory.

Link to comment
Share on other sites

That is a nice function Northern Flame.  And can see how I can use that in some other apps I am writing, thank you for that.  I could also use that just to pull the .jpg files, this way there is no chance I pull a .doc and try to process that.  Have to give that some thought.

 

I was able to find this little bit of code which seems to the trick, the only thing is what happens when I have a sub-directory?

<?php
if ($folder = opendir('/home/photoshop/rawphotos/')) {
    while (false !== ($file = readdir($folder))) {
        echo "$file\n";
    }
    closedir($handle);
}
?>

 

Could someone explain to me how the

false !== ($file = readdir($folder))
works?  Does the
($file = readdir($folder))
just return a true value if there is a file and a false value if there isn't?

 

 

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.