教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP file_put_contents()实现批量下载图片文件和css中图片代码

PHP file_put_contents()实现批量下载图片文件和css中图片代码

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供PHP file,put,contents()实现批量下载图片文件和css中图片代码等资源,欢迎您收藏本站,我们将为您提供最新的PHP file,put,contents()实现批量下载图片文件和css中图片代码资源
我们先来个个介绍一下关于php 批量下载图片文件和css中图片的方法,这个我们都会用到了file_put_contents()函数了,下面看第一个批量下载图片。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1879')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1879>set_time_limit(0);//设置PHP超时时间
$imagesURLArray = array_unique($imagesURLArray );
 
foreach($imagesURLArray as $imagesURL) {
    echo $imagesURL;
    echo "<br/>";
    file_put_contents(basename($imagesURL), file_get_contents($imagesURL));

}原理很简单,通过一个含有图片地址的数组循环,然后使用PHP的file_get_contents函数取得图片,在使用file_put_contents函数把图片保存下来。
P.S:一定要加上设置PHP超时时间哦~!

附上原文中通过preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray);
 
//note 循环需要下载的地址,逐个下载
$imagesURLArray = array_unique($imagesURLArray[1]);
    foreach($imagesURLArray as $imagesURL) {
    file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}

?>

延伸到


在扒皮过程中,必不可少的需要下载样式文件中的图片。碰到比较庞大的样式文件,其中可能会有上百个需要下载的图片,那么使用下面这段小代码是最为合适的了。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2684')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2684>

  
    < ?php

 
    //note 设置PHP超时时间


 
    set_time_limit(0);
 
 
    //note 取得样式文件内容

    $styleFileContent = file_get_contents('images/style.css');


    //note 匹配出需要下载的URL地址

    preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray);


    //note 循环需要下载的地址,逐个下载


    $imagesURLArray = array_unique($imagesURLArray[1]);

    foreach($imagesURLArray as $imagesURL) {

    file_put_contents(basename($imagesURL), file_get_contents($imagesURL));

 
    }

 

最后预祝各位在扒皮的过程中,一扒到底


关于file_put_contents定义和用法
file_put_contents() 函数把一个字符串写入文件中。

与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。

语法
file_put_contents(file,data,mode,context)

<table class="dataintable"> <tbody> <tr> <th>参数</th> <th>描述</th> </tr> <tr> <td>file</td> <td>必需。规定要写入数据的文件。如果文件不存在,则创建一个新文件。</td> </tr> <tr> <td>data</td> <td>可选。规定要写入文件的数据。可以是字符串、数组或数据流。</td> </tr> <tr> <td>mode</td> <td>

可选。规定如何打开/写入文件。可能的值:

<ul class="listintable"> <li>FILE_USE_INCLUDE_PATH</li> <li>FILE_APPEND</li> <li>LOCK_EX</li> </ul> </td> </tr> <tr> <td>context</td> <td>

可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。

</td> </tr> </tbody> </table>

您可能感兴趣的文章:
PHP file_put_contents()实现批量下载图片文件和css中图片代码
PHP批量下载html与css中图片文件实例
php下载css中图片的实例代码
php下载css中图片函数
php实现图片批量下载到本地实例(可采防盗链)
jQuery图片自动放大插件 Greyscale Hover Effect w/ CSS & jQuery
PHP采集网页图片保存到本地的示例代码
php绘图不显示图片怎么办
jQuery实现等比例缩放大图片让大图片自适应页面布局
php实现对图片对称加解密(适用身份证加密等场景)

[关闭]
~ ~