You are here: Home / Topics / How to download a file in PHP using Curl

How to download a file in PHP using Curl

Filed under: PHP on 2023-07-02 21:09:16

Here you will find how to download a file using PHP by Using PHP Curl:
 

<?php 
 $url = 'https://www.mcqbuddy.com/img/user.jpg'; 
   
 // Initialize the cURL session
 $session = curl_init($url); 
   
 // Initialize the directory name where the file will be saved
 $dir = './'; 
   
 $file_name = basename($url); 
   
 // Save file
 $save = $dir . $file; 
   
 // Open file
 $file = fopen($save, 'wb'); 
   
 // defines the options for the transfer
 curl_setopt($session, CURLOPT_FILE, $file); 
 curl_setopt($session, CURLOPT_HEADER, 0); 
   
 curl_exec($session); 
   
 curl_close($session); 
   
 fclose($file); 
?>

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.