Home > Backend Development > PHP Tutorial > 详解PHP如何下载采集图片到本地(附代码实例)

详解PHP如何下载采集图片到本地(附代码实例)

PHPz
Release: 2022-12-12 10:57:24
forward
16542 people have browsed it

本篇文章给大家介绍PHP相关知识,主要内容是教大家怎么使用php下载采集的图片到本地并保存,希望对需要的朋友有所帮助!

详解PHP如何下载采集图片到本地(附代码实例)

readfile和file_put_contents下载远程图片到本地

<?php
function download_image($pic_url)
{
    $time = time();
    $pic_local_path = dirname(__FILE__) . &#39;/cache&#39;;
    $pic_local = $pic_local_path . &#39;/&#39; . $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;
}
Copy after login

curl下载远程图片到本地【推荐学习:PHP视频教程

<?php
$ch = curl_init();
$fp=fopen(&#39;./girl.jpg&#39;, &#39;w&#39;);
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[&#39;size_download&#39;]) {
    echo "下载失败";
    echo $error;
} else {
    echo "下载成功";
}
curl_close($ch);
Copy after login
/**
 * 下载远程图片到本地
 *
 * @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 = &#39;&#39;, $dirName, $fileType = array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;), $type = 1)
{
    if ($url == &#39;&#39;)
    {
        return false;
    }
 
    // 获取文件原文件名
    $defaultFileName = basename($url);
 
    // 获取文件类型
    $suffix = substr(strrchr($url, &#39;.&#39;), 1);
    if (!in_array($suffix, $fileType))
    {
        return false;
    }
 
    // 设置保存后的文件名
    $fileName = $fileName == &#39;&#39; ? time() . rand(0, 9) . &#39;.&#39; . $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 . &#39;/&#39; . date(&#39;Y&#39;, time()) . &#39;/&#39; . date(&#39;m&#39;, time()) . &#39;/&#39; . date(&#39;d&#39;, time());
    $dirName = $dirName . &#39;/&#39; . date(&#39;Ym&#39;, time());
    if (!file_exists($dirName))
    {
        mkdir($dirName, 0777, true);
    }
 
    // 保存文件
    $res = fopen($dirName . &#39;/&#39; . $fileName, &#39;a&#39;);
    fwrite($res, $file);
    fclose($res);
 
    return array(
        &#39;fileName&#39; => $fileName,
        &#39;saveDir&#39; => $dirName
    );
}
Copy after login

PHP读写大 二进制 文件

不必申请很大内存(fopen、fread、fwrite、fclose)

<?php
/**
 * 读写大二进制文件,不必申请很大内存
 * 只有读取到内容才创建文件
 * 保证目录可写
 *
 * @param string $srcPath 源文件路径
 * @param string $dstPath 目标文件路径
 * @return bool
 */
function fetch_big_file($srcPath, $dstPath)
{
    set_time_limit(0); // 设置脚本执行时间无限长
 
    if (!$fpSrc = fopen($srcPath, "rb"))
    {
        return false;
    }
 
    $isWriteFileOpen = false; // 写文件 是否已打开?
    do
    {
        $data = fread($fpSrc, 8192); // 每次读取 8*1024个字节
        if (!$data)
        {
            break;
        }
        else if (!$isWriteFileOpen)
        {
            // 第一次读取文件,并且有内容,才创建文件
            $fpDst = fopen($dstPath, "wb");
            $isWriteFileOpen = true;
            fwrite($fpDst, $data);
        }
        else
        {
            // 写入
            fwrite($fpDst, $data);
        }
    } while (true);
 
    fclose($fpSrc);
    fclose($fpDst);
 
    return true;
}
 
$srcPath = &#39;d:/big.pdf&#39;;
$dstPath = &#39;Z:/big.pdf&#39;;
 
fetch_big_file($srcPath, $dstPath);
 
echo &#39;success&#39;;
Copy after login
Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template