Click here to Skip to main content
14,387,302 members
Rate this:
Please Sign up or sign in to vote.
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 3:28am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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[^].
   
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.
Abdul Rahman 8-Feb-18 6:06am
   
<?php
$it = new RecursiveDirectoryIterator("D:/xampp/htdocs/Tests/search_images_inDirectory/folder");
$display = Array ( 'jpeg', 'jpg' );

foreach(new RecursiveIteratorIterator($it) as $file)
{
$dest = 'D:/xampp/htdocs/Tests/search_images_inDirectory/imgs/';

if (in_array(strtolower(array_pop(explode('.', $file))), $display))
echo $file . " \n";

$destpath = $dest + "/" + basename($file);
//copy($file, $dest);
//move_uploaded_file($file, "$destpath");
if (!move_uploaded_file($file, "destpath")) {
echo "Failed.";
}
else{
echo "Success";
}

}
?>

This is the latest code which echoes all the image files..but not copying to the destination path..
Jochen Arndt 8-Feb-18 6:46am
   
The copy() command requires that
- the source is a full path to an existing file (e.g. c:\folder\file.ext)
- you have read access to the source file
- the destination is a full path (e.g. c:\other-folder\file.ext)
- the destination directory (full path without file name) exists
- you have write access to the destination directory

So print out both parameters and check if the above applies.
Abdul Rahman 8-Feb-18 7:07am
   
Every permission issues are ok. I am hell confused why this copy() function does not work.
Thanks for your promt responses.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100