使用PHP从其他网站下载文件到Web服务器

7

是否有可能直接将大于200MB的文件下载到我的Web托管上,以便我不必将该文件下载到我的计算机上,然后使用我的FTP客户端上传。由于我没有使用SSH,因此无法使用wget。我在考虑使用PHP、PERL或CGI等方式。

+==============+                                  +--------+
|  Big server  | -----------+                +--->|web host|
+==============+            |   +------+     |    +--------+
                            +-->| MyPC |-----+        |
                                +------+              |     +========+
                                                      +---->| client |
                                                            +========+

或者

+============+
| Big Server | ---+
+============+    |                      +----------+
                  +--------------------->| Web Host |
                                         +----------+
                                            |
   +------+                                 |      +========+
   | MyPC |                                 +----->| client |
   +------+                                        +========+

请帮忙……


1
你对你的Web服务器有什么样的访问权限?只有管理员面板,还是具有Shell访问权限? - Marc B
你的服务器上有什么可用的东西?你拥有什么权限? - helle
我没有 shell 访问权限,只有 FTP。 - voldyman
3个回答

8

对于 cURL

$url = "http://path.com/file.zip";
$fh = fopen(basename($url), "wb");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_exec($ch);
curl_close($ch);

1
这最终成功了,因为我的主机关闭了80端口的出站流量。代码中没有错误... - voldyman

5
在 PHP 中,最简单的方法可能是:
<?php
copy('http://server.com/big.file','/local/path/big.file');
?>

不过您应该能够执行wget命令,特别是当您的服务器禁用了外部fopen时(这种情况非常普遍)

可以使用以下PHP代码:

<?php 
chdir('/where/i/want/to/download/the/file/');
system('wget http://server.com/big.file');
?>

或者

<?php
system('wget -O /where/i/want/to/save http://server.com/big.file');
?>

curl是另一种方式。您可以执行shell命令或使用curl php。

同时确保您要下载到的文件夹(或文件)可写。


看起来像是服务器限制。 - The Surrican

2

使用PHP可以通过以下方式下载文件:

<?php
$in = fopen('http://example.com/', 'r');
$out = fopen('local-file', 'w');
while(!feof($in)) {
  $piece = fread($in, 2048);
  fwrite($out, $piece);
}
fclose($in);
fclose($out);
?>

这需要两个条件:
  • 本地文件必须可被Web服务器写入
  • Web服务器必须激活allow_url_fopen功能

我稍微修改了页面(akx.x10.mx/akx/down.php),我可以从我的服务器下载文件,但是其他服务器会出现以下错误:“failed to open stream: Connection timed out in/***/”。编辑:哦,刚看到allow_url_fopen被禁用了.......(:( - voldyman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接