С помощью 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');
}
}
Все три метода имеют примерно одинаковое время выполнения и использование памяти.
Используйте все, что подходит для вашей среды.