当前位置:网站首页>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);
边栏推荐
- 软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
- Jerry's ad series MIDI function description [chapter]
- Rust language - receive command line parameter instances
- [nonlinear control theory]9_ A series of lectures on nonlinear control theory
- onie支持pice硬盘
- [非线性控制理论]9_非线性控制理论串讲
- Games101 Lesson 7 shading 1 Notes
- word中把帶有某個符號的行全部選中,更改為標題
- Fundamentals of C language 9: Functions
- 上线APS系统,破除物料采购计划与生产实际脱钩的难题
猜你喜欢
QT color is converted to string and uint
Simulation of Michelson interferometer based on MATLAB
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
Do you really think binary search is easy
Pangolin Library: control panel, control components, shortcut key settings
烧录场景下的源代码防泄密方案分享
Opencv learning notes 8 -- answer sheet recognition
Opencv learning notes 9 -- background modeling + optical flow estimation
[MySQL learning notes 32] mvcc
Jerry's ad series MIDI function description [chapter]
随机推荐
After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
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
数据治理:主数据的3特征、4超越和3二八原则
Le chemin du navigateur Edge obtient
edge瀏覽器 路徑獲得
珠海金山面试复盘
How to prevent Association in cross-border e-commerce multi account operations?
octomap averageNodeColor函数说明
C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
Interview Reply of Zhuhai Jinshan
Ble of Jerry [chapter]
Basics of reptile - Scratch reptile
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
js对象获取属性的方法(.和[]方式)
How can word delete English only and keep Chinese or delete Chinese and keep English
TS 类型体操 之 循环中的键值判断,as 关键字使用
数据治理:数据质量篇
Google may return to the Chinese market after the Spring Festival.
861. Score after flipping the matrix
P3047 [USACO12FEB]Nearby Cows G(树形dp)