Php files getting downloaded

Download a file from the URL in PHP

In this post, I will try to explain to you how you can download any file by its URL with the help of PHP. You can do it in many ways but in this tutorial, I will explain to you a few tricks.

First Method

We will use file_get_contents() a built-in function of PHP. This function is similar to file() the only difference is file_get_contents() returns the file in a string. This function uses memory mapping techniques and it is a preferred way to read file content.

file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = 0 [, int $maxlen ]]]] ) : string

The function returns the read data or FALSE on failure.

The above function will save the file on the same path where you run the script of PHP. If you want to download the file in your desired location then you need to set some headers. That is why I write a function given below that you can use to save file form URL into your local system.

The usage of the above function is given below.

Second Method.

In this method, I will show you how you can download a file with the helo of CURL another built-in function of PHP. If you use the below function you can save the file directly into your system by giving your desired location.

Читайте также:  Check mysql version using php

The usage of the above function is given below.

$urlPdf = 'http://www.africau.edu/images/default/sample.pdf'; dfCurl($urlPdf);

You can use any above function to download the file into your system or into your server.

Источник

How to download file from URL using PHP

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

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

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.

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 для phpstorm

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

References

Источник

Оцените статью