2011-07-28 58 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

相關問題