0

I have an array whose values are image names:

Array
(
    [rocks] => rocks.jpg
    [stone] => stone.jpg
    [bird] => bird.jpg
    ...
) 

I want to scan 3 directories which are all in the same folder and match any images that match any of the array's values. The directory structure is like:

images
    nature
    animals
    misc.

I know about scandir() but am unsure of how to account for more than one directory. I eventually want to copy these matched images into a new directory. Would this be possible with PHP?

Any ideas or examples would be very helplful.

Thanks.

2
  • You could maybe directly use the underlying OS commands. For example find . -name 'rocks.jpg' (and execute that via exec()) Commented Jun 11, 2014 at 13:37
  • @feeela: There is no need for what you suggest, you can use glob. And therefore it remains 'OS independent' Commented Jun 11, 2014 at 13:41

4 Answers 4

3

You could use glob to find your files, though you will be somewhat limited to the current structure.

For example:

$files = [];
foreach($filenames as $filename) {
 $files = array_merge($files, glob('images/*/'.$filename));
}

The glob will search for anything matching '$filename' inside any subdirectory of images. If you want to go deeper than this you have to create a recursive function.

Sign up to request clarification or add additional context in comments.

4 Comments

Exactly, the glob function is what he is looking for.
This is a decent solution, though I guess it's worth mentioning that of course this will find images in any subdirectory, so if you wanted to limit it to those subdirs mentioned in the OP for example (I'm not sure of the intention) something else might be more suitable.
Thank you very much, this works great for me ... but any thoughts on how to copy the images within $files into a new directory?
@user3142418: Use the copy function.
1

You could use the file_exists function in PHP. Something like this:

$directories = array('nature', 'animals', 'misc.');
$found_images = array();
foreach ($image_names as $image) {
    foreach ($directories as $dir) {
        if (file_exists('images/' . $dir . '/' . $image) {
            $found_images[] = 'images/' . $dir . '/' . $image;
        }
    }
}

After this, $found_images will contain paths to all the images that were found.

To copy the file to another directory just use copy:

foreach ($found_images as $image) {
    copy($image, 'DESTINATION_DIRECTORY/' . basename($path));
}

2 Comments

Any thoughts on how I can deal with an item that has more than one image?
Also, although this works, why am I getting the error Warning: copy(): The first argument to copy() function cannot be a directory?
1

You can simply use two nested loops for this:

foreach(array_values($images) as $img) {
    foreach(array(
        'images/nature',
        'images/animals',
        'images/misc.'
    ) as $path) {
        if(file_exists("$path/$img")) {
            echo "file $path/$img exists" . PHP_EOL;
        }
    }
}

Comments

0

This will load all the files inside directories (Recursive) into the array

$FILES = array();

function listFolderFiles($dir){
    global $FILES;

    $ffs = scandir($dir);
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            if(is_dir($dir.$ff))
                listFolderFiles($dir.$ff.'/');
            else{
                $FILES[] = $dir.$ff;
                if(in_array($ff, $my_array))
                {
                    copy('source_dir'.$ff, 'destination_file');
                    // do your stuff here
                }
            }
        }
    }
}

listFolderFiles('your_dir');

:) enjoy!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.