如何使用curl和PHP保存文件?
答案 0 :(得分:32)
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
:是本地系统上的新文件名
答案 1 :(得分:9)
您可以使用:
<?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
答案 2 :(得分:-3)
我认为curl有-o选项将输出写入文件而不是stdout。
在-o之后你必须提供输出文件的名称。
示例:
curl -o path_to_the_file url