PHP:如何将上传的临时文件复制到多个地方?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2427936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:26:40  来源:igfitidea点击:

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

phpfile-upload

提问by ookla

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.

我也尝试使用 move_uploaded_file,但我无法让它工作。我想从上传的文件中生成 2 个拇指。

Some help?

一些帮助?

Thanks,

谢谢,

回答by Pascal MARTIN

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

move_uploaded_file将移动文件,而不是复制它——这意味着它只能工作一次。

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)

如果您正在使用copy,则可以复制的次数根本不应该有任何限制:上传创建的临时文件只会在脚本执行结束时被销毁(除非您移动/删除它之前,当然)


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


不过,也许解决方案是先使用move_uploaded_file,然后,copy
有点像,我想:

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...

这将允许您获得move_uploaded_file...提供的支票


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


如果这不起作用,那么请确保:

  • $foldercontains 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 $_FILESto $fileis done properly)
  • $folder包含你想要的——包括最终的 /
  • $file['tmp_name']也包含你想要的东西(我猜这是某种副本$_FILES——确保$_FILESto的副本$file正确完成)

回答by Chad Birch

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.

为什么不起作用move_uploaded_file()?你想用它两次吗?你不能这样做,它会移动它,所以第二次会失败。

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()一次,然后从您刚刚将其移动到的位置制作第二个副本:

move_uploaded_file($uploaded, $destination);
copy($destination, $destination2);

回答by David V.

I don't have a reply to your question directly but how about this workaround ?

我没有直接回复您的问题,但是这种解决方法如何?

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

回答by ookla

Thanks man, you give me the light.

谢谢你,你给了我光明。

I made something like this:

我做了这样的事情:

    $objUpload = new Upload();
    $filename = $objUpload->uploadFile($newFile,$folder); 
// returns a string    
    $objUpload->makeThumb($filename,$folder,"thumbs",139); 
// makes a 139px thumbnail from the original file uploaded on the first step
    $objUpload->makeThumb($filename,$folder,"mini",75); 
// makes another thumb from the same file

Using move_ulploaded_file and copy we can make only one thumb. :)

使用 move_ulploaded_file 和 copy 我们只能制作一个拇指。:)