我使用的是以下函数:
function getImg($url, $filename)
{
$hander = curl_init();
$fp = fopen($filename,'wb');
curl_setopt($hander,CURLOPT_URL,$url);
curl_setopt($hander,CURLOPT_FILE,$fp);
curl_setopt($hander,CURLOPT_HEADER,0);
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($hander,CURLOPT_TIMEOUT,60);
curl_exec($hander);
curl_close($hander);
fclose($fp);
Return true;
}
该函数把图片下载到了与页面同一目录下。现在我需要把图片下载到另一指定目录下分类管理,怎么设置保存路径呢?
curl获取远程图片时,如何设置本地保存路径?
答案:2 悬赏:40
解决时间 2021-11-12 21:14
- 提问者网友:痞子房西
- 2021-11-12 16:46
最佳答案
- 二级知识专家网友:猖狂的痴情人
- 2021-11-12 16:58
curl获取远程图片时,设置本地保存路径代码如下:
*@通过curl方式获取指定的图片到本地
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getImg($url = "", $filename = "")
{
//去除URL连接上面可能的引号
//$url = preg_replace( '/(?:^['"]+|['"/]+$)/', '', $url );
$hander = curl_init();
$fp = fopen($filename,'wb');
curl_setopt($hander,CURLOPT_URL,$url);
curl_setopt($hander,CURLOPT_FILE,$fp);
curl_setopt($hander,CURLOPT_HEADER,0);
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
//curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
curl_setopt($hander,CURLOPT_TIMEOUT,60);
curl_exec($hander);
curl_close($hander);
fclose($fp);
Return true;
}
*@通过curl方式获取指定的图片到本地
*@ 完整的图片地址
*@ 要存储的文件名
*/
function getImg($url = "", $filename = "")
{
//去除URL连接上面可能的引号
//$url = preg_replace( '/(?:^['"]+|['"/]+$)/', '', $url );
$hander = curl_init();
$fp = fopen($filename,'wb');
curl_setopt($hander,CURLOPT_URL,$url);
curl_setopt($hander,CURLOPT_FILE,$fp);
curl_setopt($hander,CURLOPT_HEADER,0);
curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
//curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
curl_setopt($hander,CURLOPT_TIMEOUT,60);
curl_exec($hander);
curl_close($hander);
fclose($fp);
Return true;
}
全部回答
- 1楼网友:桑稚给你看
- 2021-11-12 17:06
我有个类似的情况。
python起的server,php用curl模拟http链接。
server里设置的server_address参数是localhost, 所以本机可以用localhost和127.0.0.1访问。但是内网其他机器用ip地址curl不到。
把server_address参数改成ip,内网其他ip可以curl到,本机用localhost curl不到,用本机ip可以curl到。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯