如何设置PHP以将文件下载到特定目录?

时间:2011-07-28 16:50:17

标签: php file curl download filepath

我正在寻找关于这个的一般指导...

我创建了一个使用cURL下载csv文件的PHP脚本。目前,它在我运行脚本时将文件下载到我的计算机。我想修改下载路径,将其路由到我的Web主机上的目录。有没有简单的方法用PHP做到这一点?

以下是下载CSV文件的PHP脚本的快速摘要:

<?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");
?>

总而言之,我希望修改脚本,这样当我运行这个PHP文件时,它会将'file_I_am_downloading.csv'下载到我主机上的特定目录,而不是先下载到我的电脑。感谢您的时间,非常感谢您提供的任何方向。

1 个答案:

答案 0 :(得分:2)

$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);
相关问题