PHP File Download Script

The following piece of PHP code can be used to download a file from a web server:

<?php
// Method 1:
function file_download($url) {
  $file_name = basename($url); 
  file_put_contents($file_name,file_get_contents($url));
}

// Method 2:
    $filepath = urldecode($_REQUEST["filename"]);
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filepath));
    flush();
    readfile($filepath);
?>

Usage:
1. Save the above code in download.php
2. Use it as: http://example.com/download.php?filename=file.csv