简体   繁体   English

使用PHP下载远程文件(2GB)

[英]Download a remote file (2GB) using PHP

I have a large file (2GB) and I want to download it to server A from server B with PHP. 我有一个大文件(2GB),我想用服务器B用PHP将它下载到服务器A.

I tried both FTP and CURL with PHP but without success. 我用PHP尝试了FTP和CURL但没有成功。 it returns a TIMEOUT REQUEST error even if I set_time_limit() with 0 value. 即使我将set_time_limit()设置为0,它也会返回TIMEOUT REQUEST错误。

Here's my code: 这是我的代码:

<?php

set_time_limit(0)

$local_file = 'file.zip';
$server_file = '/www/file.zip';

$conn_id = ftp_connect("ftp.webmashing.com");

$login_result = ftp_login($conn_id, "USERNAME", "PASSWORD");

if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}

ftp_close($conn_id);

?>

If you are having timeouts uploading a file, you might have a look at ftp_pasv() 如果你有超时上传文件,你可能会看看ftp_pasv()

bool ftp_pasv ( resource $ftp_stream , bool $pasv )

In your case, try this: 在你的情况下,试试这个:

$conn_id = ftp_connect("ftp.webmashing.com");

$login_result = ftp_login($conn_id, "USERNAME", "PASSWORD");

ftp_pasv($conn_id, true);

When called, set_time_limit() restarts the timeout counter from zero. 调用时,set_time_limit()从零重新启动超时计数器。 In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out. 换句话说,如果超时是默认的30秒,并且脚本执行25秒,则进行诸如set_time_limit(20)之类的调用,脚本将在超时之前运行总共45秒。 Please check the "max_execution_time" directive in php.ini 请检查php.ini中的“max_execution_time”指令

If you can (depends on where your php files are executed), you can change the max_execution_time to be more longer. 如果可以(取决于您的php文件的执行位置),您可以将max_execution_time更改为更长。

Now, what I would do instead (still, if you have the rights), is to do it in command line called from the php files. 现在,我会做什么(仍然,如果你有权利),是在从php文件调用的命令行中做。 The command line would write the results in a specific file and all you have to do would be to check the content of that file. 命令行会将结果写入特定文件,您所要做的就是检查该文件的内容。

Once the result is written (say "0" for success as always in linux), you can do what you want. 一旦结果被写入(比如说“0”就像在linux中一样成功),你可以做你想要的。

On the front end, a little ajax to get the state of the downloading could be usefull too ! 在前端,获得下载状态的一点点ajax也很有用!

But as always, it depends on if you can run shell commands. 但是一如既往,这取决于你是否可以运行shell命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM