Friday, October 24, 2025
HomeLanguagesHow 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
  
      $url = 'https://www.geeksforgeeks.org/contribute/';
      $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:

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS