Wednesday, July 3, 2024
HomeLanguagesPhpHow to download files from an external server with code in PHP...

How to download files from an external server with code in PHP ?

PHP provides many inbuilt variables or functions to perform this kind of operation. One of them is file_get_contents to download the files from the external server using PHP.

file_get_contents() Function Parameters:

  • $path: It declares the path of the file which we are going to fetch.
  • $include_path: This is a binary parameter, set 1 to find the file in the included path.
  • $context: It specifies how we change the modes of the filehandle.
  • $start: It is the starting line to fetch.
  • $max_length: It specifies the max length of the file.

In this, we are going to understand how to download the files and how to download the content of the particular page from the external server. 

Example 1: In this, we see how to download the file using the file_get_contents() method.

PHP




<?php
    
    $URL
        
    $file = basename($URL);
      
    $success = file_put_contents($file, file_get_contents($URL));
      
    if ($success) {
        echo "File downloaded successfully from the server ";
    }
    else {
        echo "File downloading failed.";
    }
?>


Output:

Example 2: In this, we understand how to download the content from the external server. We set the URL in the variable in which we want to fetch the content. We open a file in write mode and then fetch the content using the cURL method and store the data in a file.

PHP




<?php
  
      $destination_file = "gfg.html";
  
      $fp = fopen($destination_file, "w+");
  
      $ch = curl_init();
        
    curl_setopt_array($ch, array(
          CURLOPT_URL=>$url, CURLOPT_FILE=>$fp ));
  
      $res = curl_exec($ch);
  
      echo "File is downloaded ..!!";
      curl_close($ch);
      fclose($fp);
?>


Output:

Example 3: In this, we are using cURL which is also called as client’s URL. We initialize the file URL and store it in the variable. We specify the file name to store the file, then open the file in w+ mode. By using cURL we first transfer the file and then execute the session. After the file is downloaded we close the session as well as the file.

PHP




<?php
  
      $file_url
  
      $destination_path = "download3.png";
      $fp = fopen($destination_path, "w+");               
  
      $ch = curl_init($file_url);
      curl_setopt($ch, CURLOPT_FILE, $fp);
      curl_exec($ch);
  
      $st_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);
      fclose($fp);
  
      if($st_code == 200)
          echo 'File downloaded successfully from the server';
      else
          echo 'Error occur';
?>


Output:

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments