使用curl和PHP保存文件 [英] Saving file using curl and PHP

查看:110
本文介绍了使用curl和PHP保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用curl和PHP保存文件?

解决方案



  function get_file($ file,$ local_path,$ newfilename)
{
$ err_msg ='';
echo< br>尝试为$ file< br>下载邮件;
$ out = fopen($ local_path。$ newfilename,wb);
if($ out == FALSE){
print文件未打开< 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>错误是:.curl_error($ ch);

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

} // end function

功能:
函数并接受三个参数

  get_file($ file,$ local_path,$ newfilename)



$ file :是要检索的对象的文件名



$ local_path :是存储对象的目录的本地路径



$ newfilename :是本地系统上的新文件名


How can I save a file using curl and PHP?

解决方案

did you want something like this ?

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 

Functionality: Its a function and accepts three parameters

get_file($file, $local_path, $newfilename)

$file : is the filename of the object to be retrieved

$local_path : is the local path to the directory to store the object

$newfilename : is the new file name on the local system

这篇关于使用curl和PHP保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆