如何设置PHP将文件下载到特定的目录? [英] How Do I Set PHP to Download File to a Specific Directory?
问题描述
我正在寻找一个这方面的一般指导...
I am looking for some general guidance on this one...
我创建了一个使用cURL下载csv文件的PHP脚本。目前,当我运行脚本时,它会将文件下载到我的电脑。我想修改下载路径将其路由到我的Web主机上的目录。有什么简单的方法可以用PHP来做到吗?
I have created a PHP script that uses cURL to download a csv file. Currently, it downloads the file to my computer when I run the script. I would like to amend the download path to route it to a directory on my web host. Is there any easy way to do this with PHP?
以下是下载CSV文件的PHP脚本的简要摘要:
Here is a quick summary of the PHP script that downloads the CSV file:
<?php
$ch = curl_init();
//this is where I submit the form to download the csv file...
curl_close ($ch);
header("Content-Disposition: attachment; filename=file_I_am_downloading.csv");
header("Content-Type: application/octet-stream");
readfile("http://www.example.com/file_I_am_downloading.csv");
?>
<它将'file_I_am_downloading.csv'下载到我的主机上的特定目录,而不是首先下载到我的电脑。感谢您的时间,您可以提供的任何方向非常感谢。
To sum this up, I would like the script to be amended so that when I run this PHP file it downloads 'file_I_am_downloading.csv' to a specific directory on my host rather than first downloading to my computer. Thank you for your time and any direction you can provide is much appreciated.
推荐答案
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
die(curl_error($ch));
}
// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);
这篇关于如何设置PHP将文件下载到特定的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!