回答
我還發現copy,它允許一個文件從URL直接複製到磁盤,是一個oneliner沒有捲曲或複雜需要創建一個空文件,在其中傳輸file_get_contents的內容。
copy($file_url, $localpath);
你不必創建一個空文件。只需提供一個可寫的文件路徑。 – 2010-09-10 18:19:40
不知道copy()解決方案...會做一些基準測試。聽起來很有趣。 – 2010-09-10 18:37:25
基準測試exec wget,copy()和curl ...所有的運行時和內存使用情況幾乎相同。編輯我的答案供以後參考。感謝您的副本() - 解決方案+1。 – 2010-09-10 19:14:04
使用CURLOPT_FILE
您可以將一些文件流直接寫入打開的文件句柄(請參閱curl_setopt)。根據您的意見
/**
* @param string $url
* @param string $destinationFilePath
* @throws Exception
* @return string
*/
protected function _downloadFile($url, $destinationFilePath)
{
$fileHandle = fopen($destinationFilePath, 'w');
if (false === $fileHandle) {
throw new Exception('Could not open filehandle');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fileHandle);
$result = curl_exec($ch);
curl_close($ch);
fclose($fileHandle);
if (false === $result) {
throw new Exception('Could not download file');
}
return $destinationFilePath;
}
編輯:
如果你想有一個oneliner或者想使用wget調用它通過exec()或system()像這樣:
exec('wget http://google.de/ -O google.html -q')
編輯以供日後參考:
<?php
function downloadCurl($url, $destinationFilePath)
{
$fileHandle = fopen($destinationFilePath, 'w');
if (false === $fileHandle) {
throw new Exception('Could not open filehandle');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fileHandle);
$result = curl_exec($ch);
curl_close($ch);
fclose($fileHandle);
if (false === $result) {
throw new Exception('Could not download file');
}
}
function downloadCopy($url, $destinationFilePath)
{
if (false === copy($url, $destinationFilePath)) {
throw new Exception('Could not download file');
}
}
function downloadExecWget($url, $destinationFilePath)
{
$output = array();
$return = null;
exec(sprintf('wget %s -O %s -q', escapeshellarg($url), escapeshellarg($destinationFilePath)), $output, $return);
if (1 === $return) {
throw new Exception('Could not download file');
}
}
這三種方法的運行時間和內存使用量幾乎相等。
使用最適合您的環境的任何東西。
一樣。我想下載該文件不要讀取它並重新創建它。 – 2010-09-10 12:05:23
Elzo:該文件直接寫入本地文件。這跟wget一樣。 – 2010-09-10 12:12:17
不是直接。我必須打開一個文件,並打開它。沒有? wget是oneliner。 – 2010-09-10 13:17:04
$c = file_get_contents('http://www.example.com/my_file.tar.gz');
現在寫$ C到本地文件...
閱讀我的評論。 file_get_contents給了我一個字符串,我必須寫入一個文件。我希望文件不要創建副本。 – 2010-09-10 12:02:37
我不明白「我想要的文件」...在一個PHP變量?在文件系統上?其中... – Riba 2010-09-10 16:53:57
是的,在文件系統上。那裏的文件留下來。你在變量或內存中有什麼不是文件 – 2010-09-10 18:10:41
file_put_contents($ LOCAL_PATH,的file_get_contents($ FILE_URL));
是一個內襯太;-)
與上面的代碼唯一的問題可能是非常大的文件:一份可能是在這種情況下更好,還能看到http://www.php.net/manual/en/function.copy.php#88520
一些測試需要...
- 1. 我怎樣才能下載使用C++
- 2. 我怎樣才能得到其他表
- 3. 我怎樣才能用ANTLR4導入其他java庫?
- 4. 我怎樣才能從其他的ColdFusion文件中的值
- 5. 我怎樣才能在ruby中下載兩個json文件
- 6. Lua Love2D - 我怎樣才能讓它下載一個文件?
- 7. 我怎樣才能使通過的XmlDocument()創建下載文件
- 8. 我怎樣才能保存excel文件在下載文件夾使用asp.netnet#
- 9. 我怎樣才能讓bjam在其他目錄中找到我的.h文件
- 10. 我怎樣才能調用webview窗體的其他屏幕
- 11. 我怎樣才能將這段代碼用於其他活動?
- 12. 我怎樣才能看到sdf文件
- 13. 我怎樣才能的application.ini文件
- 14. 我怎樣才能打開FLP文件?
- 15. 我怎樣才能從PSD文件
- 16. 我怎樣才能
- 17. 我怎樣才能
- 18. 我怎樣才能
- 19. 我怎樣才能
- 20. 我怎樣才能讓ReSharper的讓我標籤其他
- 21. 我怎樣才能加載我的pdf文件從SD卡
- 22. 我怎樣才能用ajax
- 23. 我怎樣才能用jQuery
- 24. 我怎樣才能在web上下文XML文件
- 25. 我怎樣才能提供一個gzipped的xml文件下載使用PHP
- 26. 我怎樣才能讓用戶下載文件時,頁面Django是看
- 27. 我怎樣才能得到其他分支git中的新文件
- 28. Git:我怎樣才能修改文件,並仍然檢查其他分支?
- 29. 我怎樣才能使CSS或JavaScript
- 30. 我怎樣才能複製串或STR
您是否嘗試過使用PHP的CURL實現? – 2010-09-10 11:29:14
我猜cURL具有寫入文件的相同方法,它從源讀取什麼。我想下載文件而不打開它並重新創建它的MIME類型和這樣的東西。 – 2010-09-10 11:36:43