In this article we will show you the solution of PHP download file from URL, now we used a PHP extension named cURL here. Let us understand the cURL first. cURL: cURL stands for Client URL and is a library that lets you make HTTP requests.
cURL is a PHP extension allowing us to receive and send information via the URL syntax.
By doing so cURL makes it easy to commination between different websites and domains.
Step By Step Guide On PHP Download File From URL :-
<?php $image = '' ; $error = '' ; if (isset($_POST["download"])) { if (empty($_POST["url"])) { $error = '<p> <b?> Please Enter URL !! </b> </p>' ; } else if (!filter_var($_POST["url"], FILTER_VALIDATE_URL)) { $error = '<p> <b?> Invalid URL !! </b> </p>' ; } else{ $url = $_POST["url"] ; $start = curl_init() ; curl_setopt($start, CURLOPT_URL, $url) ; curl_setopt($start, CURLOPT_RETURNTRANSFER, 1) ; curl_setopt($start, CURLOPT_SSLVERSION, 3) ; $file_data = curl_exec($start) ; curl_close($start) ; $file_path = 'destination/'.uniqid().'jpg' ; $file = fopen($file_path, 'w+') ; fputs($file, $file_data) ; fclose($file) ; $image = '<img src="'.$file_path.'">' ; } } ?> <!DOCTYPE html> <html lang = " en " > <head> <meta charset = " UTF - 8" > <meta http-equiv = " X-UA-Compatible " content = " IE=edge " > <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " > <title> php download file from url </title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> php download file from url </h2> <form method="post"> <label for="name">Enter URL</label> <input type="text" name="url" id="name" autocomplete="off"> <?php echo $error; ?> <input type="submit" name="download" value="download"> <?php echo $image; ?> </form> </body> </html>
- At first <?php tag to write the php.
- First create two variables for $image and $error.
- Create an if statement with the isset () function with $_POST method. And creating another if else statement within it
- If the entered URL is empty using echo to display an error message
- Use filter_validate_url() to see if the url is valid or not. If not display an error message of Invalid URL.
- Else if the URL is valid, then created $url with $_POST method
- Now creating a variable $start to curl_init() for initialize the cURL.
- Use curl_setopt() to set a option for a curl transfer.
- $file_data to curl_exec($start) for execution of the start
- Curl_close() to close the curl
- $file_path to add the filepath using uniqid() and set the format to jpg
- Fopen() to open the file path
- Fputs() to put the $file_data into $file.
- Fclose() to close the file
- $image to add the file path to display the image on the HTML page.
- Close the php code with ?> tag
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Creating a form using <form> tag with the action of the pdf file with the method POST.
- Two <input> tags for the input for the URL and Submit the URL.
- Echo to display error message and the image we have downloaded.
- Using an external CSS file to style the form
- By clicking on the submit button we can download the file
- Close the HTML tag with </html>
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know how to download file from URL using PHP.
I hope this article on PHP download file from URL helps you and the steps and method mentioned above are easy to follow and implement.