当前位置:网站首页>What are the ways to download network pictures with PHP
What are the ways to download network pictures with PHP
2022-07-06 07:46:00 【Brother Jing】
Method 1
advantage : Simple .
shortcoming : Relatively inefficient .
This method mainly uses two functions file_get_contents as well as file_put_contents This method is very simple , Generally speaking, first get To picture content , then put To a file .
The code is as follows :<?php
$imgFile = 'http://***.***.***/Uploads/20200424/1587710975CZlGeB.jpg';
// Get the file name
$imgName = trim(strrchr($imgFile, '/'), '/');
file_put_contents($imgName, file_get_contents($imgFile));
Method 2
The method most used by individuals .
shortcoming : A little more complicated , To master curl Usage of , Need to know curl The meaning of some options .
advantage : Efficient , It's easy to support https;
curl Very powerful , It's also phper Skills that must be mastered , Therefore, it is recommended that you use this method . The idea of this method is , Use curl Tools to get information about remote pictures , Then save the information to a local file . I'm not right here curl_exec The result of is verified , Steal laziness . About curl Usage of , If you want to know more , Can be in Gxlcms Mid search curl.$imgFile = 'https://***.***.***/uploads/20200121/bd873d945595.png';
function down_img($imgUrl)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imgUrl); // Request address
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Do not output information directly
// You can visit https request
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// perform curl And get the data
$results = curl_exec($ch);
$imgName = trim(strrchr($imgUrl, '/'), '/');
$fp = fopen($imgName, 'w');
fwrite($fp, $results);
// After execution , Remember to close resources
curl_close($ch);
}
down_img($imgFile);
Method 3
It's also very simple , What is used is php Knowledge of document management . First Create two file handles , A binary information for reading remote pictures , Another one is used to save picture information .<?php
$imgFile = '
$imgName = trim(strrchr($imgFile, '/'), '/');
// Read remote image resources
$source = fopen($imgFile, 'rb');
// Create a local file resource handle , Used to save picture information
$down = fopen($imgName, 'wb');
while ($chunk = fread($source,1024)){
fwrite($down, $chunk, 1024);
}
fclose($source);
fclose($down);
边栏推荐
- How to delete all the words before or after a symbol in word
- Scala语言学习-08-抽象类
- 洛谷P1836 数页码 题解
- Is the super browser a fingerprint browser? How to choose a good super browser?
- Sélectionnez toutes les lignes avec un symbole dans Word et changez - les en titre
- Launch APS system to break the problem of decoupling material procurement plan from production practice
- Iterator Foundation
- Scala language learning-08-abstract classes
- esRally国内安装使用避坑指南-全网最新
- Le chemin du navigateur Edge obtient
猜你喜欢
Solution: intelligent site intelligent inspection scheme video monitoring system
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
MEX有关的学习
Significance and measures of encryption protection for intelligent terminal equipment
Typescript interface and the use of generics
Ble of Jerry [chapter]
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems
Summary of Digital IC design written examination questions (I)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]
Key value judgment in the cycle of TS type gymnastics, as keyword use
随机推荐
Ble of Jerry [chapter]
Typescript indexable type
珠海金山面试复盘
二叉树创建 & 遍历
Notes on software development
[CF Gym101196-I] Waif Until Dark 网络最大流
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
Onie supports pice hard disk
Related operations of Excel
In the era of digital economy, how to ensure security?
[MySQL learning notes 32] mvcc
Position() function in XPath uses
解决方案:智慧工地智能巡檢方案視頻監控系統
How to estimate the number of threads
Typescript function definition
861. Score after flipping the matrix
Word delete the contents in brackets
Get the path of edge browser
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
C intercept string