Home > Backend Development > PHP Tutorial > Two ways to quickly download files from URL in PHP

Two ways to quickly download files from URL in PHP

藏色散人
Release: 2023-04-10 16:06:02
Original
10097 people have browsed it

In the previous article "Example demonstrates how to call a PHP function when a button is clicked", I introduced to you how to call a PHP function when a button is clicked. Friends in need can learn about it~

The topic of this article is how to download files from URL through PHP?

There are actually many ways to download files from URLs, but this article will focus on two methods:

The first method:

Use file_get_contents () Function: The file_get_contents() function is used to read files into strings. This function uses server-supported memory mapping technology, which improves performance and makes it the preferred way to read file contents.

Syntax:

file_get_contents($path, $include_path, $context, $start, $max_length)
Copy after login

Create a test.php sample file with the following content:

<?php
// 将文件URL初始化为变量
$url = &#39;https://img.php.cn/upload/article/000/000/020/6125a391e4e76689.png&#39;;

// 使用basename()函数获取文件名
$file_name = basename($url);

// 使用file_get_contents()函数从url获取文件,使用file_put_contents()函数保存文件
if(file_put_contents( $file_name,file_get_contents($url))) {
    echo "文件下载成功";
}
else {
    echo "文件下载失败";
}
Copy after login

The running results show:

文件下载成功
Copy after login

Now we can Check whether the "6125a391e4e76689.png" file was successfully saved in the same directory as the test.php file:

Two ways to quickly download files from URL in PHP

The file was successfully saved from the URL.

Second method: Use PHP Curl

Create a test.php sample file, the sample code is as follows:

<?php

$url = &#39;https://img.php.cn/upload/article/000/000/020/6125a391e4e76689.png&#39;;

// 初始化cURL会话
$ch = curl_init($url);

// 初始化保存文件的目录名
$dir = &#39;./&#39;;

// 函数返回文件名
$file_name = basename($url);

// 将文件保存到给定位置
$save_file_loc = $dir . $file_name;

// 打开文件
$fp = fopen($save_file_loc, &#39;wb&#39;);

// 为cURL传输设置一个选项
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

// 执行cURL会话
curl_exec($ch);

// 关闭cURL会话并释放所有资源
curl_close($ch);

// 关闭文件
fclose($fp);
Copy after login

Run the test.php file Then you can view the downloaded and saved file at the specified location.

This method implements the specific steps to download files:

  • Initialize the file URL as a variable

  • Create a cURL session

  • Declare a variable and store the name of the directory where the downloaded file will be saved.

  • If the file path is provided as a parameter, use the basename() function to return the file base name.

  • Save the file to the given location.

  • Open the saved file location by writing a string

  • Set cURL transfer options

  • Execute the cURL session and close the cURL session and release all resources

  • Close the file

Finally, I recommend the latest and most comprehensive "PHP Video tutorial》~Come and learn!

The above is the detailed content of Two ways to quickly download files from URL in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template