Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to search for all images in folders and its sub folders and if found images {with extension png jpeg gif}, Need to copy [not move] it to a specific folder.
Can anyone help me? I have tried this code.
Thank you

What I have tried:

<?php

$dir_path = "folder/";
$extensions_array = array('jpg','png','jpeg');

if(is_dir($dir_path))
{
    $files = scandir($dir_path);
    
    
    for($i = 0; $i < count($files); $i++)
    {
        if($files[$i] !='.' && $files[$i] !='..')
        {
            // get file name
            echo "File Name -> $files[$i]<br>";
            
            // get file extension
            $file = pathinfo($files[$i]);
            $extension = $file['extension'];
            //$extension = pathinfo($info->getFilename(), PATHINFO_EXTENSION);
            echo "File Extension-> $extension<br>";
            
           // check file extension
            if(in_array($extension, $extensions_array))
            {
            // show image
            echo "<img src='$dir_path$files[$i]' style='width:100px;height:100px;'><br>";
            }
        }
    }
}
Posted
Updated 7-Feb-18 2:28am

1 solution

You have to implement a recursive file search which can be done with the PHP: RecursiveDirectoryIterator - Manual[^]. Within the iteration check for matching extensions (which can be also done using a filter) and use the PHP: copy - Manual[^] function.

I suggest to try it yourself using the PHP documentation to increase your skills. Otherwise you can find many examples in the web when breaking down the problem and searching for each part: recursive file search, checking if a file extension (string) matches an item from a list, and copying a file.

Example: Recursive File Search (PHP) - Stack Overflow[^].
 
Share this answer
 
Comments
Abdul Rahman 8-Feb-18 0:37am    
Thanks for your reply. But when I tried like this it fails..plz help..

<?php
$it = new RecursiveDirectoryIterator("D:/xampp/htdocs/Tests/search_images_inDirectory/folder");
$display = Array ( 'jpeg', 'jpg' );

$dest = 'D:/xampp/htdocs/Tests/search_images_inDirectory/imgs';
foreach(new RecursiveIteratorIterator($it) as $file)
{
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
echo $file . " \n";
copy($file, $dest);

}
?>
Jochen Arndt 8-Feb-18 3:13am    
What does "it fails" mean?

However, your code would copy every file because the copy() command is out of the extension check condition and you have to create the full destination path for the copy destination:
$destpath = $dest + "\" + basename($file);
Abdul Rahman 8-Feb-18 5:06am    
//copy(): The first argument to copy() function cannot be a directory //
This is the error I get.
Jochen Arndt 8-Feb-18 5:34am    
That is probably due to the error I mentioned in my above post. It must be
if (in_array(strtolower(array_pop(explode('.', $file))), $display)) {
echo $file . " \n";
copy($file, $dest);
}

The parentheses are missing in your code so that copy() is called for every found file including directory names.
Abdul Rahman 8-Feb-18 6:04am    
it echos all the files..But i cannot copy those images to another specified folder..
i have given destination folder as you said..
$destpath = $dest + "\" + basename($file);..
but the copy command does not work..
is this due to php versions? i am using 5.6
Thanks for your all replies.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900