How much time for two tasks

Hi

how much time will it take if i rename 2000 images on server.

rename($old,$new)

how much time will it take if i copy 2000 images to another folder.

copy($old, $new)

how much time will it take if i do both tasks together ??

will there be any timeout ??

whats the best solution to do both tasks without any timeout ??

vineet

Well, rename is essentially the same as copy in terms of resource use.

copy is like get_original_file → save_another_file_with_the_same_name
rename is like get_original_file → save_another_file_with_a_different_name

How long it would take depends on How many files, How large the files are (weight, not dimensions), How busy the server is, How much memory is available.

Unless you’re dealing with small weight image files I recommend you do it in batches.
Especially if you’re on a shared host and you don’t want to risk getting them upset.

With those caveats in mind, there is
ignore_user_abort
that could deal with the time out concern.

But again, if you do this, do it wisely.

hi

i tried doing it in batches.
i inserted for loop in the code but it doesnt works.

if i remove the forloop then the code works fine

<?php
define('IMAGEPATH', 'graphics/gnew/');
define('IMAGEPATH2', 'graphics/gnew2/');
foreach(glob(IMAGEPATH.'*') as $fnm){
    $n =  $fnm;
    $n2 = IMAGEPATH2.basename($fnm);

    if(file_exists($n2))
    {
    echo $n2. " already exists <br>";
    }
    else{
        for($i=0;$i<2;$i++)
        {
        copy($n, $n2);
        echo $n." copied <br>";
        }
        
    }
}
?>

whats wrong in my code ?? it works without for loop ??

how to insert limit in the code to process only 2 images ??

vineet

i tried it with array_slice and It updates only two records. Thats fine.

But it stops after copying 2 images and doesnt copy next 2 images on page refresh.

But i want to copy 2 images everytime i refresh page.

so how do i copy next 2 images on refreshing the page.

<?php
define('IMAGEPATH', 'graphics/gnew/');
define('IMAGEPATH2', 'graphics/gnew2/');

foreach(array_slice(glob(IMAGEPATH.'*'),0,2) as $fnm)
    {
    $n =  $fnm;
    $n2 = IMAGEPATH2.basename($fnm);
    
    copy($n, $n2);
    echo $n." copied <br>";
    }

?>

vineet

You’ll need to keep a note of how many images you’ve already copied, and start the loop at the next one. Your code is fine, but each time you load it, it will try to copy the same two images that it’s already copied. So one easy way might be instead of refreshing, have the code create a form with a submit button that contains a hidden form variable you can use as the loop start point. When you press the button, it re-calls the same code, which can read the start-point from the form variables, add 2 to it, do the job, and re-draw the form with the new start value. Or you could stick it in a session variable.

1 Like

what will i store in the variable ??
can provide a basic example ??

vineet

You’d store the starting point in the hidden variable.

<?php
define('IMAGEPATH', 'graphics/gnew/');
define('IMAGEPATH2', 'graphics/gnew2/');
if (isset($_POST['startpoint'])) { 
  $start = $_POST['startpoint'];
  }
else
  {
  $start = 0;
  }
foreach(array_slice(glob(IMAGEPATH.'*'),$start,2) as $fnm)
    {
    $n =  $fnm;
    $n2 = IMAGEPATH2.basename($fnm);
    copy($n, $n2);
    echo $n." copied <br>";
    }
// output your form
$start += 2; // or however many you want to do
echo '<form method="post" action="whatever-your-page-is">';
echo '<input type="hidden" name="startpoint" value="' . $start . '">';
echo '<input type="submit" value="Next copy">';
echo '</form>';
   
?>

I haven’t put all the opening and closing HTML tags that you’ll probably need, assuming you have that covered.

perfect. thanks droopsnoot for the code.

one thing more i would like to ask.

if i have 2000 images and i want to get the “total size of all 2000 images” then how will i get it ??

it could be 500mb, 700mb or what ever ??

But how to get or calculate total size of all 2000 images ??

vineet

sum up filesize() on every path.

It is not. And rather the opposite. Just try it in your home PC and see.

Rename will take only a fraction of time because it takes to rewrite only disk index. While copying will be writing the actual contents of the every file.

@vinpkl if you need to do both tasks, then you need only the latter, because you can always give the new name when copy.

For only 2000 files better do it in one batch, without any HTML stuff to interfere.

1 Like

I actually wrote a very similar thing, but I deleted it because, reading the doc for the PHP rename function, it does very much give the impression that it’s copying the file and deleting the original. Maybe it varies from OS to OS, but the fact it can rename from one disk partition to another suggests it is copying.

PHP is but a tiny wrapper around system calls or third-party apis. And for sure rename() doesn’t do anything of its own but calling an os routine. And every os renames a file by means of just rewriting an index table if happens within the same logical disc/partition or copy/erase otherwise.

Fair enough. It intrigued me, but not enough to start looking around to see if was easy to find source. It’s strange, though, that the manual page suggests that there was an enhancement to the php rename() function to support renaming between different disks, which if it only calls the underlying OS rename, would not be something that the php code has any influence on.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.