用php實(shí)現(xiàn)的獲取網(wǎng)頁中的圖片并保存到本地的代碼
更新時間:2010年01月05日 23:23:22 作者:
將網(wǎng)頁中圖片保存本地文件夾
復(fù)制代碼 代碼如下:
<?php
header("Content-type:image/jpeg");
function read_url($str)
{
$file=fopen($str,"r");
while(!feof($file))
{
$result.=fgets($file,9999);
}
fclose($file);
return $result;
}
function save_img($str)
{
$result=read_url($str);
$result=str_replace("\"","",$result);
$result=str_replace("\'","",$result);
preg_match_all('/<img\ssrc=(http:\/\/.*?)(\s(.*?)>|>)/i',$result,$matches);
foreach($matches[1] as $value)
{
echo $value."<br>\n";
//GrabImage($value,$filename="");
}
}
// $url 是遠(yuǎn)程圖片的完整URL地址,不能為空。
// $filename 是可選變量: 如果為空,本地文件名將基于時間和日期
// 自動生成.
function GrabImage($url,$filename="") {
if($url==""):return false;endif;
$path="download/"; //指定存儲文件夾
//若文件不存在,則創(chuàng)建;
if(!file_exists($path)){
mkdir($path);
}
if($filename=="") {
$ext=strrchr($url,".");
if($ext!=".gif" && $ext!=".jpg"):return false;endif;
$filename=$path.date("dMYHis").$ext;
}
ob_start();
readfile($url);
$img = ob_get_contents();
ob_end_clean();
$size = strlen($img);
$fp2=@fopen($filename, "a");
fwrite($fp2,$img);
fclose($fp2);
return $filename;
}
save_img("http://www.dbjr.com.cn");
?>
您可能感興趣的文章:
相關(guān)文章
laravel 實(shí)現(xiàn)設(shè)置時區(qū)的簡單方法
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)設(shè)置時區(qū)的簡單方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10將FCKeditor導(dǎo)入PHP+SMARTY的實(shí)現(xiàn)方法
這篇文章主要介紹了將FCKeditor導(dǎo)入PHP+SMARTY的實(shí)現(xiàn)方法,涉及整合FCKeditor與SMARTY的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-01-01學(xué)習(xí)php設(shè)計模式 php實(shí)現(xiàn)訪問者模式(Visitor)
這篇文章主要介紹了php設(shè)計模式中的訪問者模式,使用php實(shí)現(xiàn)訪問者模式,感興趣的小伙伴們可以參考一下2015-12-12