php教程

PHP采集图片Curl远程下载到本地教程

php教程 51源码 2024-01-24 人阅读

readfile和file_put_contents方法

<?php
function download_image($pic_url)
{
    $time = time();
    $pic_local_path = dirname(__FILE__) . '/cache';
    $pic_local = $pic_local_path . '/' . $time;
    if (!file_exists($pic_local_path)) {
        mkdir($pic_local_path, 0777);
        @chmod($pic_local_path, 0777);
    }
    ob_start(); //打开输出
    readfile($pic_url); //输出图片文件
    $img = ob_get_contents(); //得到浏览器输出
    ob_end_clean(); //清除输出并关闭
    file_put_contents($pic_local, $img);
    return $pic_local;
}

curl方法

<?php
$ch = curl_init();
$fp=fopen('./girl.jpg', 'w');
curl_setopt($ch, CURLOPT_URL, "https://img02.sogoucdn.com/app/a/100520091/20181209114105");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FILE, $fp);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
fclose($fp);
$size = filesize("./girl.jpg");
if ($size != $info['size_download']) {
    echo "下载失败";
    echo $error;
} else {
    echo "下载成功";
}
curl_close($ch);

下面是一个函数方法

/**
 * 下载远程图片到本地
 *
 * @param string $url 远程文件地址
 * @param string $filename 保存后的文件名(为空时则为随机生成的文件名,否则为原文件名)
 * @param array $fileType 允许的文件类型
 * @param string $dirName 文件保存的路径(路径其余部分根据时间系统自动生成)
 * @param int $type 远程获取文件的方式
 * @return json 返回文件名、文件的保存路径
 * @author blog.snsgou.com
 */
function download_image($url, $fileName = '', $dirName, $fileType = array('jpg', 'gif', 'png'), $type = 1)
{
    if ($url == '')
    {
        return false;
    }
    // 获取文件原文件名
    $defaultFileName = basename($url);
    // 获取文件类型
    $suffix = substr(strrchr($url, '.'), 1);
    if (!in_array($suffix, $fileType))
    {
        return false;
    }
    // 设置保存后的文件名
    $fileName = $fileName == '' ? time() . rand(0, 9) . '.' . $suffix : $defaultFileName;
    // 获取远程文件资源
    if ($type)
    {
        $ch = curl_init();
        $timeout = 30;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file = curl_exec($ch);
        curl_close($ch);
    }
    else
    {
        ob_start();
        readfile($url);
        $file = ob_get_contents();
        ob_end_clean();
    }
    // 设置文件保存路径
    //$dirName = $dirName . '/' . date('Y', time()) . '/' . date('m', time()) . '/' . date('d', time());
    $dirName = $dirName . '/' . date('Ym', time());
    if (!file_exists($dirName))
    {
        mkdir($dirName, 0777, true);
    }
    // 保存文件
    $res = fopen($dirName . '/' . $fileName, 'a');
    fwrite($res, $file);
    fclose($res);
    return array(
        'fileName' => $fileName,
        'saveDir' => $dirName
    );


版权声明:文章搜集于网络,如有侵权请联系本站,转载请说明出处:https://www.51yma.cn/jiaocheng/php/1408.html
文章来源:
标签 采集教程