我试图下载一个文件,要求您点击网站上的“下载”。
https://apps.tdi.state.tx.us/sfsdatalookup/StartLookupAction.do?inquiryId=30
如何编写php代码以便在调用脚本时自动下载文件?
答案 0 :(得分:0)
file_put_contents("filename", fopen("https://apps.tdi.state.tx.us/sfsdatalookup/StartLookupAction.do?inquiryId=30", 'r'));
答案 1 :(得分:0)
如果您尝试使用脚本下载文件,那么@Gudgip答案是正确的,但您需要检查php.ini选项allow_url_fopen
设置true
您可以下载带有curl扩展名的文件(必须启用,请在curl
时查看$ php -m
)
$url = 'https://apps.tdi.state.tx.us/sfsdatalookup/StartLookupAction.do?inquiryId=30';
$temp_file = tempnam('/tmp', 'file_'); // return something like '/tmp/file_BQDfep'
$fp = fopen($temp_file, 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
// close the file
fclose($fp);
// now you have the file downloaded in '/tmp/file_BQDfep'
如果您想要在用户点击该网址时从其他URI下载文件,请尝试
$file_url = 'http://remote.server.cim/file-to-download.exe';
$base_name = basename($file_url);
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header(sprintf('Content-disposition: attachment; filename="%s"', $base_name));
readfile($file_url);
答案 2 :(得分:0)
您可以拥有标题并使用这样的读取文件。
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=yourfile.txt");
readfile("https://apps.tdi.state.tx.us/sfsdatalookup/StartLookupAction.do?inquiryId=30");