Home > Article > Backend Development > PHP grabs the images in the content and downloads the saved code
PHP grabs the images in the web page content and downloads and saves the implementation code to the specified directory. Friends in need can refer to it.
The complete code is as follows. <?php //$url 远程完整图片地址,不能为空 //程序员之家 http://bbs.it-home.org //$filename 另存为的图片名称,保存在与脚本相同的目录中 function getImage($url, $filename=""){ if($url == ""){return false;} $ext = strrchr($url, ".");//获取图片的扩展名 if($ext != ".gif" && $ext != ".jpg" && $ext != ".bmp"){ echo "格式不支持!";return false; } if($filename == ""){ $filename = time()."$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; } //测试 getImage("http://bbs.it-home.org/images/123.jpg", "zq123.jpg"); ?> For articles about information in php crawled content, you can also refer to: php file_get_contents code to capture page information php file_get_contents function code to capture page information php code for grabbing email addresses from web pages |