php

Download file from remote url to user desktop in PHP

<?php
    $file_url = $_GET['file_url'];
    $file_name = basename($file_url);
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"".$file_name."\"");
    readfile($file_url);
    exit;
?>

To download a file on a user desktop from a remote URL in PHP, you can use this code snippet. We are using readfile() PHP function for that and also Content-disposition header which is required to download the file.

Was this helpful?