Online Forum To Support Coders With Technical Assistance - Read Write Execute

How to download files using url in php

Ankit Agrawal
Ankit Agrawal
Published on January 30, 2019 · 1 min read

You can download files directly using url or you can your files server to server using following code

1 : Using file_put_contents
set_time_limit(0);
file_put_contents("files.zip", fopen("Source Url", 'r'));
2 : Using curl
set_time_limit(0); 
$aFopen = fopen('files.zip', "w"); 
$aOpts = array(
  CURLOPT_FILE    => $aFopen,
  CURLOPT_TIMEOUT =>  18000, // Set timeout duration in second
  CURLOPT_URL     => 'Source Url',
);

$curl = curl_init();
curl_setopt_array($curl, $aOpts);
curl_exec($curl);
curl_close($curl);
Was this article helpful?
Ankit Agrawal

Ankit Agrawal

Author & Senior Engineer at CodeExecute

Writing practical engineering guides, debugging real-world bottlenecks, and creating free tools for developer productivity.

← Previous Article How to generate pdf in PHP using FPDF Next Article → How to add watermark text on image in php