Download a file in PHP

This post was written by Brandon on May 1, 2010
Posted Under: PHP

Here is a simple function to download a file in PHP and save it on your web server. I needed this for another project, so I coded it and decided to post it up here!
The $file variable is the url to the file
And the $savefile variable is where it should be saved on the server (path and file name)

function get_file($file, $savefile)
    $out = fopen($savefile, 'wb');
    if ($out == FALSE){
      print "Error opening file<br>";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FILE, $out);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $file);
    echo curl_error ( $ch);

Add a Comment

required, use real name
required, will not be published
optional, your blog address