0

I want to copy images from one folder to another on server, now I use this code:

<?php
function read_dir($dir)
{
   $list = array();
   if (is_dir($dir)) 
   {
     if ($handle = opendir($dir)) 
     {
        while (false !== ($file = readdir($handle))) 
        {
           if ($file != "." && $file != "..") 
           {
               $list[] = $file;
           }
        }
     }
     closedir($handle);
   }
   return $list;
}
$src="oldfolder";
$dest="newfolder";
$list= read_dir($src);
foreach($list as $key => $val)
{
   copy("$src/$val","$dest/$val");
}
echo "Done";
?>

But I need to copy just images selected by time - for example images uploaded between "now" and 5 min. ago..

Can anyone help? Thanks

Now my PHP is like below. It seems that it run with "Done" result, but nothing is copied..

<?php
function read_dir($dir)
{
   $list = array();
   if (is_dir($dir)) 
   {
     if ($handle = opendir($dir)) 
     {
        while (false !== ($file = readdir($handle))) 
        {

         $fpath = 'oldfolder'.$file;
         if (file_exists($fpath)) {

           if($file != "." && $file != ".." &&                 
DateTime::createFromFormat('U', filemtime($file)) < new DateTime("-5 
minutes")) 
           {
               $list[] = $file;
          }
          }
        }
     }
     closedir($handle);
   }
   return $list;
}
$src="oldfolder";
$dest="newfolder";
$list= read_dir($src);
foreach($list as $key => $val)
{
   copy("$src/$val","$dest/$val");
}
echo "Done"; 
?>
  • [filemtime()](http://php.net/manual/en/function.filemtime.php) – Jeff Jun 26 '18 at 08:59
  • Maybe [filemtime()](https://secure.php.net/manual/en/function.filemtime.php) can help – brombeer Jun 26 '18 at 09:00
  • Possible duplicate of [PHP: how can I get file creation date?](https://stackoverflow.com/questions/4401320/php-how-can-i-get-file-creation-date) – CD001 Jun 26 '18 at 09:01
  • And how can I implement this, or, where can I find some example – Kamera Prvá Jun 26 '18 at 09:02
  • You can find examples in the links we've posted and in the linked SO Question. – Jeff Jun 26 '18 at 09:08
  • Just tweak the `if ($file != "." && $file != "..")` condition to include it ... e.g. `if($file != "." && $file != ".." && DateTime::createFromFormat('U', filemtime($file)) > new DateTime("-5 minutes")) { ... }` - since it's all timestamps you probably don't need to use `DateTime` but it just comes more naturally to me to do so – CD001 Jun 26 '18 at 09:12
  • I tried it, but become warning: filemtime(): stat failed for ... Any solution? – Kamera Prvá Jun 26 '18 at 10:12
  • Might want to include a [file_exists](http://php.net/manual/en/function.file-exists.php) check before doing `filemtime()` - [more info here](https://stackoverflow.com/questions/13386082/filemtime-warning-stat-failed-for) – CD001 Jun 26 '18 at 13:00
  • I made some edit (shown up) but no succsess – Kamera Prvá Jun 26 '18 at 14:47

2 Answers2

1

So this is my code, that works for me well - copy images between folders according to time (- 5 sec) set by other code in "time.txt" file:

 <?php
 function read_dir($dir)
 {
 $list = array();
 if (is_dir($dir)) 
 {
 if ($handle = opendir($dir)) 
 {
 while (false !== ($file = readdir($handle))) 
 {
 $fpath = 'oldfolder/'.$file;
 if (file_exists($fpath)) {

 $subor = fopen("./time.txt", "r"); 
 $cas_txt=fgets($subor, 11);
 fclose($subor);
 $cas_zac = DateTime::createFromFormat('U', $cas_txt)->modify('-5 seconds');
 if ($file != "." && $file != ".." && DateTime::createFromFormat('U', 
 filemtime($fpath)) > $cas_zac)
 {
 $list[] = $file;
 }
 }
 }
 }
 closedir($handle);
 }
 return $list;
 }
 $src="oldfolder";
 $dest="newfolder";
 $list= read_dir($src);
 foreach($list as $key => $val)
 {
 //copy file to new folder
 copy("$src/$val","$dest/$val");
 }
 echo "Done";
 ?>

I have two more questions:

Please how can I rotate images in 180° by or after copy? Is it possible in one php code?

How can I send multiple files - images from my code - like an attachments by mail in php?

Thanks for your help.

0

You should use the filemtime function

fred727
  • 2,020
  • 15
  • 15