从URL下载文件到服务器 [英] Download File to server from URL
问题描述
file_put_contents(Tmpfile.zip,file_get_contents(http ://someurl/file.zip));
只有一个问题。如果你有一个大文件,如100mb,怎么办?然后,你的内存不足,无法下载文件。
我想要的是在下载文件时将文件写入磁盘的方式它。
由于PHP 5.1.0, file_put_contents()
支持通过传递流句柄逐个写作为 $ data
参数:
file_put_contents(Tmpfile.zip ,fopen(http://someurl/file.zip,'r'));
从手册中:
如果 数据 [这是第二个参数]是流资源,该流的剩余缓冲区将被复制到指定的文件。这与使用
stream_copy_to_stream()
类似。
(感谢 Hakre 。)
Well, this one seems quite simple, and it is. All you have to do to download a file to your server is:
file_put_contents("Tmpfile.zip", file_get_contents("http://someurl/file.zip"));
Only there is one problem. What if you have a large file, like 100mb. Then, you will run out of memory, and not be able to download the file.
What I want is a way to write the file to the disk as I am downloading it. That way, I can download bigger files, without running into memory problems.
Since PHP 5.1.0, file_put_contents()
supports writing piece-by-piece by passing a stream-handle as the $data
parameter:
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
From the manual:
If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using
stream_copy_to_stream()
.
(Thanks Hakre.)
这篇关于从URL下载文件到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!