PHP 7 Script to Download Files and Images From URL to Server Using cURL Method Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be downloading files and images from url to server using curl method in php 7.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

index.php

 

 

<?php

// Initialize a file URL to the variable
$url = 'https://contribute.geeksforgeeks.org/wp-content/uploads/gfg-40.png';

// Initialize the cURL session
$ch = curl_init($url);

// Inintialize directory name where
// file will be save
$dir = './';

// Use basename() function to return
// the base name of file
$file_name = basename($url);

// Save file into file location
$save_file_loc = $dir . $file_name;

// Open file
$fp = fopen($save_file_loc, 'wb');

// It set an option for a cURL transfer
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

// Perform a cURL session
curl_exec($ch);

// Closes a cURL session and frees all resources
curl_close($ch);

// Close file
fclose($fp);

?>

Leave a Reply