2011-07-28 60 views
3

我正在寻找这方面的一些一般指导...如何设置PHP将文件下载到特定目录?

我已经创建了一个PHP脚本,使用cURL下载一个csv文件。目前,当我运行脚本时,它将文件下载到我的电脑。我想修改下载路径将其路由到我的虚拟主机上的目录。有没有简单的方法来做到这一点与PHP?

下面是PHP脚本的简单总结说下载的CSV文件:

<?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'到主机上的特定目录,而不是先下载到我的电脑。感谢您的时间和任何方向,你可以提供非常感谢。

+0

如果您fopen封装使您可以简单地使用PHP copy命令:http://php.net/manual/en/function.copy.php – JohnP

回答

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); 
+0

我试过了,但它给我一个错误:Warning:file_put_contents(/ file/path /)[function.file-put-contents]:无法打开流:权限被拒绝...我将777分配到目录,我不确定为什么我得到错误。我会继续努力。感谢您的输入:) – Presto

+0

您需要指定一个文件名,而不仅仅是一个目录。 '/ some/path/file.txt' –

+0

太棒了!现在工作出色了:)谢谢 – Presto

相关问题