PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to download file from URL using PHP

March 9, 2022
Reading Time: 3 mins read
PHP101.net - How to download file from URL using PHP

PHP101.net - How to download file from URL using PHP

Share on FacebookShare on Twitter

Building PHP applications will require file interaction a lot, one of them is download file from URL using PHP. This article will guide you the very basic methods of using PHP for downloading file from an URL.

To download file from URL using PHP

There are two general methods to download file from URL using PHP:

1. Using PHP file_get_contents() and file_put_contents() function:

This method can only be used if the web hosting allows the file_get_contents function to run. A lot of the web hosting turns off this function for security reasons, so you should check if the function is enabled before using this method.

After successfully getting the file, file_put_contents will be used to actually save the file into a location.

<?php

// Set the URL for downloading
$fileUrl = 'https://php101.net/wp-content/uploads/2021/12/php101-logo-horizontal-250x74-1.png';

// Get the file name and extension using basename()
$fileName = basename( $fileUrl );

// Specify file save path
$savePath = '/dir/' . $fileName;

// Use file_get_contents to get the file, use @ to hide possible errors
$file = @file_get_contents( $fileUrl );

// Use file_put_contents to save the file using the path we defined, and check if there are any errors
if ( file_put_contents( $savePath, $file ) ) {
    echo 'File downloaded successfully';
} else {
    echo 'File failed to download';
}

If the message tells that the download is successful, the file will be store on the save path we defined.

2. Using PHP CURL and fopen()

The CURL method is more widely used, and we recommend you to use PHP CURL for downloading files from URLs instead of using file_get_contents function.

CURL provides more compatibility, more controls over the downloading process and helps you to get familiar with using CURL in PHP, which will be crucial for many other network-related tasks in PHP. Also, working with fopen will be more convenient later with file interaction tasks.

To download file from URL using PHP with CURL and fopen:

<?php

// Set the URL for downloading
$fileUrl = 'https://php101.net/wp-content/uploads/2021/12/php101-logo-horizontal-250x74-1.png';

// Initialize CURL
$ch = curl_init( $url );

// Get the file name and extension using basename()
$fileName = basename( $fileUrl );

// Specify file save path
$savePath = '/dir/' . $fileName;

// Initialize fopen pointer to store the file.
// 'w' is write mode, and 'b' is binary mode, can be excluded if the file is a text-based file
$fp = fopen( $savePath, 'wb' );

// Set the file transfer option and header to CURL
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_HEADER, 0 );

// Execute the CURL session
curl_exec( $ch );

// Close the session after running to free resources
curl_close( $ch );

// Close file pointer
fclose( $fp ); 

If no error displays after running the codes, the file will be stored at the defined $savePath location.

Final thoughts

The tutorial is now over. Hopefully it is helpful for you to understand the basic knowledge to download file from URL using PHP with file_get_contents and CURL.

Thank you for reading!

You might also like

  • How to write comments with PHP (and best practices)
  • How to add elements to array easily with PHP

References

  • Official PHP.net Documentation – file_get_contents function
  • Official PHP.net Documentation – file_put_contents function
  • Official PHP.net Documentation – fopen function
  • Official PHP.net Documentation – fclose function
  • Official PHP.net Documentation – CURL
Tags: download file with phpphp curlphp download filephp download file from urlphp file_get_contentsphp file_put_contentsphp fopen
Previous Post

How to install Composer on Linux

Next Post

How to convert from array to string in PHP

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • To download file from URL using PHP
    • 1. Using PHP file_get_contents() and file_put_contents() function:
    • 2. Using PHP CURL and fopen()
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.net - How to convert array to string in PHP

How to convert from array to string in PHP

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to install Composer on Windows

How to get user IP address using PHP

How to detect visitors by country using PHP & GeoIP

3 ways to get PHP memory limit value

How to display image from file using PHP

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.