Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how do I copy a temp file upload to multiple places?

Tags:

how can I copy two times the same file? I'm trying to do something like this:

                copy($file['tmp_name'], $folder."1.jpg");
            copy($file['tmp_name'], $folder."2.jpg");
            copy($file['tmp_name'], $folder."3.jpg");

And how many time does temp files has before it's destroyed by the server?

I try using move_uploaded_file also, but I can't make it work. I want to generate 2 thumbs from an uploaded file.

Some help?

Thanks,

like image 707
ookla Avatar asked Mar 11 '10 19:03

ookla


2 Answers

move_uploaded_file will move the file, and not copy it -- which means it'll work only once.

If you are using copy, there shouldn't be any limit at all on the number of times you can copy : the temporay file created by the upload will only be destroyed at the end of the execution of your script (unless you move/delete it before, of course)


Still, maybe a solution would be to use move_uploaded_file first, and, then, copy ?
A bit like that, I suppose :

if (move_uploaded_file($file['tmp_name'], $folder . '1.jpg')) {
    copy($folder . '1.jpg', $folder . '2.jpg');
    copy($folder . '1.jpg', $folder . '3.jpg');
}

This would allow you to get the checks provided by move_uploaded_file...


If this doesn't work, then, make sure that :

  • $folder contains what you want -- including the final /
  • That $file['tmp_name'] also contains what you want (I'm guessing this is some kind of copy of $_FILES -- make sure the copy of $_FILES to $file is done properly)
like image 177
Pascal MARTIN Avatar answered Oct 21 '22 06:10

Pascal MARTIN


Why doesn't move_uploaded_file() work? Are you trying to use it twice? You can't do that, it moves it, so the second time will fail.

I would just use move_uploaded_file() once, and then make the second copy from the location you just moved it to:

move_uploaded_file($uploaded, $destination);
copy($destination, $destination2);
like image 38
Chad Birch Avatar answered Oct 21 '22 06:10

Chad Birch