All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Download File From URL

Last Updated : Mar 11, 2024

PHP Download File From URL

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>
  1. At first <?php tag to write the php.
  2. First create two variables for $image and $error.
  3. Create an if statement with the isset () function with $_POST method. And creating another if else statement within it
  4. If the entered URL is empty using echo to display an error message
  5. Use filter_validate_url() to see if the url is valid or not. If not display an error message of Invalid URL.
  6. Else if the URL is valid, then created $url with $_POST method
  7. Now creating a variable $start to curl_init() for initialize the cURL.
  8. Use curl_setopt() to set a option for a curl transfer.
  9. $file_data to curl_exec($start) for execution of the start
  10. Curl_close() to close the curl
  11. $file_path to add the filepath using uniqid() and set the format to jpg
  12. Fopen() to open the file path
  13. Fputs() to put the $file_data into $file.
  14. Fclose() to close the file
  15. $image to add the file path to display the image on the HTML page.
  16. Close the php code with ?> tag
  17. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  18. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  19. 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.
  20. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  21. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  22. <h1> tag used to add heading here and also adding the inline CSS here.
  23. Creating a form using <form> tag with the action of the pdf file with the method POST.
  24. Two <input> tags for the input for the URL and Submit the URL.
  25. Echo to display error message and the image we have downloaded.
  26. Using an external CSS file to style the form
  27. By clicking on the submit button we can download the file
  28. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪