Forcing to Download a File Using PHP

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.

Forcing to download a file using PHP

.htaccess Solution

To brute force all CSV files on your server to download, add in your .htaccess file:

AddType application/octet-stream csv

PHP Solution

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");

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.

With PHP how to force download of file with randon file name and type

First and foremost create a download handler file which will be accepting parameters.

I'll call it download.php

download.php

<?php
ignore_user_abort(true); // prevents script termination
set_time_limit(0); // prevent time out

$file = isset($_GET['file']) ? $_GET['file'] : ''; //get filename

if ($file) {
$path_info = pathinfo($_GET['file']);
$file_name = $path_info['basename'];
$dir = "uploads"; //directory

$path_to_file = $dir.'/'.$file_name; //full path

if(!file_exists($path_to_file)) { // check if file exist or terminate request

exit('the file does not exist');
}

if(!is_readable($path_to_file)) { //check if file is readable from the directory

exit("security issues. can't read file from folder");

}

// set download headers for file

$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: ' . finfo_file($finfo, $path_to_file));

$finfo = finfo_open(FILEINFO_MIME_ENCODING);
header('Content-Transfer-Encoding: ' . finfo_file($finfo, $path_to_file));

header('Content-disposition: attachment; filename="' . basename($path_to_file) . '"');

readfile($path_to_file); // force download file with readfile()
}

else {

exit('download paramater missing');
}
?>

Usage

<a href="download.php?file=randomfilename.pdf">My pdf </a>

Hope this helps.

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.

php file force download

I've had a chance to work it out. Your problem is two-fold.

First, remove the www. from the url.

Second, remove the call to filesize($file) which is throwing an error because PHP doesn't know the size of the file before it downloads the file. (really, just remove the whole line)

Removing these two things, I was successful.

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.

Write a text file and force to download with php

use readfile() and application/octet-stream headers

<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
?>

Force Downloading file through php not working

Try the code below

<?php 
$file_name = 'file.png';
$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);
?>

Read more , Read this tutorial too



Related Topics



Leave a reply



Submit