Сохранение файла с использованием curl и PHP - PullRequest
10 голосов
/ 17 июня 2009

Как я могу сохранить файл, используя curl и PHP?

Ответы [ 3 ]

32 голосов
/ 17 июня 2009

ты хотел что-то подобное?

function get_file($file, $local_path, $newfilename) 
{ 
    $err_msg = ''; 
    echo "<br>Attempting message download for $file<br>"; 
    $out = fopen($local_path.$newfilename,"wb");
    if ($out == FALSE){ 
      print "File not opened<br>"; 
      exit; 
    } 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_FILE, $out); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_URL, $file); 

    curl_exec($ch); 
    echo "<br>Error is : ".curl_error ( $ch); 

    curl_close($ch); 
    //fclose($handle); 

}//end function 

Функциональность: Это функция и принимает три параметра

get_file($file, $local_path, $newfilename)

$file: имя файла объекта для извлечения

$local_path: локальный путь к каталогу для хранения объекта

$newfilename: это новое имя файла в локальной системе

9 голосов
/ 17 июня 2009

Вы можете использовать:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$out = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);


$fp = fopen('data.txt', 'w');
fwrite($fp, $out);
fclose($fp);

?>

См .: http://jp2.php.net/manual/en/function.curl-exec.php и http://us3.php.net/manual/en/function.fwrite.php

0 голосов
/ 17 июня 2009

Я думаю, что в curl есть опция -o для записи вывода в файл вместо stdout.

После -o вы должны указать имя выходного файла.

пример:


curl -o path_to_the_file url
...