我需要使用CURL从URL保存图像并将其保存到服务器上的文件夹中。我一直在努力与这段代码无济于事。理想情况下,我想获取图像并将其另存为“ photo1”或其他内容。帮帮我!
function GetImageFromUrl($link) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$link); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result=curl_exec($ch); curl_close($ch); return $result; } $sourcecode = GetImageFromUrl($iticon); $savefile = fopen(' /img/uploads/' . $iconfilename, 'w'); fwrite($savefile, $sourcecode); fclose($savefile);
尝试这个:
function grab_image($url,$saveto){ $ch = curl_init ($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $raw=curl_exec($ch); curl_close ($ch); if(file_exists($saveto)){ unlink($saveto); } $fp = fopen($saveto,'x'); fwrite($fp, $raw); fclose($fp); }
并确保在php.ini中启用allow_url_fopen