How to Automatically Start a Download in PHP

How to Automatically Start a Download in PHP?

Send the following headers before outputting the file:

header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/octet-stream");
header("Content-Length: " . filesize($File));
header("Connection: close");

@grom: Interesting about the 'application/octet-stream' MIME type. I wasn't aware of that, have always just used 'application/force-download' :)

How to force a file to download in PHP

If you want to force a download, you can use something like the following:

<?php
// Fetch the file info.
$filePath = '/path/to/file/on/disk.jpg';

if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);

// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);

// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
?>

If you simply link to this script using a normal link the file will be downloaded.

Incidentally, the code snippet above needs to be executed at the start of a page (before any headers or HTML output had occurred.) Also take care if you decide to create a function based around this for downloading arbitrary files - you'll need to ensure that you prevent directory traversal (realpath is handy), only permit downloads from within a defined area, etc. if you're accepting input from a $_GET or $_POST.

PHP automatically download file

        <?php
// set url "http://
curl_setopt($ch, CURLOPT_URL, "<your url>");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

file_put_contents($output,"<file>";
?>

Start download automatically when a user navigates to another page

Redirect to a page which emits the following headers:

header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: $length");

See this post about the restrictions on $filename.

edit in response to andr's answer, the php equivalent of redirect-after-x-seconds would be:

header("Refresh: 2; url=start_download.php");

(although you should officially specify a complete URL, I think) where start_download.php would contain the two lines above.

Downloading zip file automatically via php code

u need to add header for the client about file downloading. just see the manual example: http://php.net/manual/en/function.header.php#example-5315

<?php
$file = getcwd() . '/read.zip';
// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"read.zip\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
ob_end_flush();
@readfile($file);


Related Topics



Leave a reply



Submit