开发者

wait for copy() to complete

is there a way for me to check to see if a file is copied before continuing to execute a php loop?

i have a for loop, and within th开发者_开发技巧e loop it is going to copy a file. now, i want it so that it waits until the current file is copied before continuing the loop.

example:

for ($i = 1; $i <= 10; $i++)
{
$temp = $_FILES['tmp_name'];
$extension = '.jpg';

copy("$temp_$i_$extension", "$local_$i_$extension");

// not sure what to do here

if (FILE_DONE_COPYING())
{
    CONTINUE_LOOP();
}
else
{
    PAUSE_LOOP();
}
}

thats just an example. i have no clue how to do this...can anyone chime in?


That's what copy() does in PHP - it blocks until the file is copied. There's nothing you need to do, except checking the return value to see if the operation was successful.


PHP is taking it line by line, step by step, so it's waiting until copy() is completed

for ($i = 1; $i <= 10; $i++)
{
    $temp = $_FILES['tmp_name'];
    $extension = '.jpg';

    $result = copy("$temp_$i_$extension", "$local_$i_$extension");

    if($result){  
        //done
    }
    else{
        //failed
    }
}


copy returns true on success and false on failure. Check for that.


Unless you go through the trouble of using threading and have copy fired asynchronously, PHP will not move to the line after copy until after it has completed.


copy does wait for completion before continuing execution. It is a syncronous call. But, it can return false if it didn't work, and your copy wont work since $temp_ and $i_ are not defined variables. So maybe you are thinking the copy isn't finishing, when it actually just isn't working at all.

You should use:

copy("{$temp}_{$i}_$extension", "{$local}_{$i}_$extension");

OR

copy($temp.'_'.$i.'_'.$extension, $local.'_'.$i.'_'.$extension);


What makes you think that copy() will return before it has finished?

You could of course compare filesize of original file and copy to be sure the process is complete.


You could use a while loop with sleep calls to delay checking, and just exit the while loop once the file exists under the new name.


I know this is an ancient question but I feel I really need to talk about this problem. COPY is a great command - BUT - it does not work all of the time. I can honestly tell you this. Why? Why does it not always work? Simple - the Operating System is at fault. Here are two examples. One is using a standard disk drive and the second one deals with a Ram disk. The COPY command reads CHUNKS of a file and writes these chunks out to the destination. This means it really does NOT just do a File_get_contents but instead does the fopen(IN), fopen(OUT), while( !EOF(IN) ){ fread(IN), fwrite(OUT) } and then fclose(IN), and fclose(out). It should be noted that these commands try to make sure everything goes ok but if the disk drive buffers what it does - then the file might take a second or two to finish. This can be seen by having a file_exists() on the output file's name. It can come back as FALSE(it IS NOT there). This is because the disk drive's hardware has not caught up with the commands.

I even installed the AMD RamDisk software and ran a program using the above commands (both file_get_contents->file_put_contents and the fopen-fread/fwrite-fclose commands). The same thing happened then also. Every now and then (not always) the file_exists() function returned FALSE because the test got there before the file had finished being created. Don't ask me why - it just would do this.

So what do I suggest? Use the SLEEP() command. Maybe use three(3) seconds (so SLEEP(3);) -after- the COPY() command. I also determined that a CHMOD(, 0777); was a good idea also. With a SLEEP() command after it so it has time to apply the changes. (Which is probably closer to one second).

Now, remember - everyone's hardware is different. So some hardware might work better or faster than the one I am using. So - this is one of those - try it if you are having problems. Or don't - if you are not having problems. It is that simple. So - this is happening to me - I'm using it - it works now that the system gets three seconds to breath - but it might not do anything for you - who has an atomic powered Willy-Wonka mobile which does the impossible before breakfast.

Got it? Good. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜