我想用php从网址下载一些zip文件。 I.E. sitename.com/path/to/file/id/123当你直接进入这个网址时,你会得到一个文件下载提示。我尝试使用fopen()
和file_get_contents()
,但这些都失败了。我已经回顾了如何使zip文件可下载或如何从sitename.com/path/to/file.zip获取文件,但我的网址没有.zip扩展名。
fopen ('url.com') or die('can not open');
浏览器显示无法打开
答案 0 :(得分:5)
网址可能是一个可能会重定向您的脚本。请改用CURL
$fh = fopen('file.zip', 'w');
$ch = curl_init()
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fh);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // this will follow redirects
curl_exec($ch);
curl_close($ch);
fclose($fh);
答案 1 :(得分:1)
使用像 readfile() 这样的基本功能可能更容易。
<?php
// We'll be outputting a ZIP
header('Content-type: application/zip');
// Use Content-Disposition to force a save dialog.
// The file will be called "downloaded.zip"
header('Content-Disposition: attachment; filename="downloaded.zip"');
// The ZIP source is in original.zip
readfile('original.zip');
?>