PHP - How to Force Download of a File

php - How to force download of a file?

You could try something like this:

$file_name = 'file.avi';
$file_url = 'http://www.myremoteserver.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;

I just tested it and it works for me.

Please note that for readfile to be able to read a remote url, you need to have your fopen_wrappers enabled.

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.

Force-downloading, from php file

THis is hard - most php configuration will fail after 30 seconds. If you own php.ini you can change that to longer limit. But still - is that even worth it? I mean - the files can get bigger or network slower - and once more you will hit the timeout.

This is why downloaders were made - to download big files in smaller chunks Half Crazed showed you code for that i THIS answer (its not only one - this only takes into account one of the ways clients negotiate the transfers - but still its a good start).

Mega.co.nz for example uses new html5 features. Downloads the file in browser using chunks, joining the file on user and and then ,,downloading'' it from the browser disk space. It can resume files, pause files and so on. (Sorry - no code for that as it would be quite big and include more than one language (php, js)).

PS: change yours readfile($path); into:

$handle=fopen($path, 'rb');
while (!feof($handle))
{
echo fread($handle, 8192);
flush();
}
fclose($handle);

This will not load WHOLE file into memory, just parts of 8KiB at once and then send them to user.

How to Force a file to download using PHP on mobile Browsers?

Try with the following headers:

header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="filename"');

The idea is to set the content type to something that the browser doesn't know how to open - that way it will show the save dialogue. You can try it with the actual MIME type, it should work, but I can't test it right now.

PHP force download, download without naming the file the full path name

In readfile you have to pass full path, but in header in filename file name for user:

ob_clean();
if(isset($_POST['file_name'])){
$file_for_user = $_POST['file_name'];
$full_path_file = "STORAGE_PATH".$file_for_user;
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$file_for_user.'"');
readfile($full_path_file);
exit();
}

How to force download different type of extension file php

To download any file extension forcefully. You can first read the file and writes that file to the output buffer for download.

Here is the code, to download files. Hopes, it will also help to fix your problem.

This code is based on: http://php.net/manual/en/function.readfile.php

<?php
// You can used any file extension to output the file for download
$file = 'monkey.gif';

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>

PHP - Force download or view in browser depending on file type

In order to display a file in the browser, you'll need to use the correct MIME type. You can set it yourself based on file extension, or you can use the finfo module:

function getContentType($filename) {
$finfo = new finfo(FILEINFO_MIME);
return $finfo->file($filename);
}

header("Content-Type: " . getContentType($filename));

Without this, the browser will probably assume that it can't handle the application/octet-stream content, and force a download anyway.

You should also only send the Content-Disposition header if you want to force the file to be downloaded. If you remove that header, then the browser can decide if it should display the file or download it.

Writing string to file and force download in PHP

You do not need to write the string to a file in order to send it to the browser. See the following example, which will prompt your UA to attempt to download a file called "sample.txt" containing the value of $str:

<?php

$str = "Some pseudo-random
text spanning
multiple lines";

header('Content-Disposition: attachment; filename="sample.txt"');
header('Content-Type: text/plain'); # Don't use application/force-download - it's not a real MIME type, and the Content-Disposition header is sufficient
header('Content-Length: ' . strlen($str));
header('Connection: close');


echo $str;


Related Topics



Leave a reply



Submit