PHP download script, downloads automatically instead of prompting as it should - php

I have a script that starts a download in PHP. When users click on the link that starts the download it should prompt the user whether they want to save the file or not. It works fine in firefox, but in Safari and Chrome the download starts automatically without prompting the user.
Here is my code
$extension = fileexten($filename);
if(($filename!= false)&&($fakename!=false&& #fopen($filename,'r')==true)){
$mime = contenttype($extension);
set_time_limit(0);
header('Pragma: public');
header('Expires: 0');
header("Content-Type:".$mime);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Disposition: attachment;filename='.$fakename.'.'.$extension);
header("Content-Transfer-Encoding: binary");
if (ob_get_length() > 0) {
ob_end_clean();
}
flush();
readfile($filename);
}
else{
$error = "<h3>We could not find this file</h3>";} // If the filename or fake filename could not be retrieved.
}
Is there anyway I can make sure the browser prompts them to save or download the file rather than it start automatically in other browsers?

It has nothing to do with a script but a browser setting.
In case of Google Chrome that's how you change it:

Related

Website becomes unusable while downloading file served by PHP script

I'm running IIS 8.0 and have a script that serves various downloads when someone clicks on a download link. However I'm running into an issue where as long as a user is downloading something the website is completely unresponsive until that download completes. Below is the code for the download script. This script is being opened in a new window.
$extension = fileexten($filename);
if(($filename!= false)&&($fakename!=false&& #fopen($filename,'r')==true)){
$mime = contenttype($extension);
set_time_limit(0);
header('Pragma: public');
header('Expires: 0');
header("Content-Type:".$mime);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Disposition: attachment;filename='.$fakename.'.'.$extension);
header("Content-Transfer-Encoding: binary");
if (ob_get_length() > 0) {
ob_end_clean();
}
flush();
readfile($filename);
}
else{
$error = "<h3>We could not find this file</h3>";} // If the filename or fake filename could not be retrieved.
}
Close the session before outputting the file.
session_write_close();
readfile($filename);
The session can only be opened by one PHP process at a time, and any other requests that issue a session_start() command will block while waiting for access to the session data file.

When serving .dmg files from PHP, user is getting 'disk image not recognized'

I have a .dmg file on my IIS server. When downloading directly the file opens just fine, but when I serve the file via PHP like so
$mime_types['dmg'] ='application/x-apple-diskimage';
$filename = getfile($_GET['dc']);
$fakename = fakefilename($_GET['dc']);
$extension = fileexten($filename);
if(($filename!= false)&&($fakename!=false&& #fopen($filename,'r')==true)){
$mime = contenttype($extension);
set_time_limit(0);
header('Pragma: public');
header('Expires: 0');
header("Content-Type:".$mime);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Disposition: attachment;filename='.$fakename.'.'.$extension);
header("Content-Transfer-Encoding: binary");
if (ob_get_length() > 0) {
ob_end_clean();
}
flush();
#readfile($filename);
}
I get an error on the mac saying 'disk image not recognized'
I've also tried setting the .dmg application/octet-stream but I still run into the same issue.
My guess is that that this is either a case in which the content type is not set correctly or in which the content length is incorrectly set. Check to see if $mime = contenttype($extension); returns the correct content type.
It would be useful to debug this with a web debugging proxy tool (like Fiddler or Charles) and post the entire response header, when accessing the file directly & when you access it via your PHP script.
UPDATE (based on the comments below):
The script had additional line breaks at the end of the file, which were being sent out in the response.

ios shows file in mobile safari rather than downloading?

I am using the following code to present a file for download to the user... in this case it is a .csv file. Works great in all browsers, BUT in IOS it loads the file in the mobile safari browser. The exact same code works fine for a .zip file (although ios gives the warning it cannot download that type of file).
What gives? Does ios completely disregard the headers or what?
if (is_file($local_path.$file))
{
//get current ts
$now = time();
// set the headers and prevent caching
header('Pragma: public');
header('Expires: -1');
header('Cache-Control: public, must-revalidate, post-check=0, pre-check=0');
header('Content-Disposition: attachment; filename="'.$now.'_'.$file.'"');
// set the mime type based on extension
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_path.$file).'');
//download
readfile($local_path.$file);
//delete file
unlink($local_path.$file);
}

PHP Downloading File(s) outside webroot

I save many documents outside the webroot.
I want to click a link, that opens a new window (target="_blank"), and force download the file that's found.
Here's what I've got so far, but my results show gobble-de-gook in the browser popup, rather than forcing the download to the desktop:
function download($filelocation){
$filename = basename($filelocation);
if (file_exists($filelocation)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filelocation));
ob_clean();
flush();
readfile($filelocation);
exit;
}
}
In the new browser window I simply call that download() function with a specific path the the file.
It's definitely finding the file, but now I'm just wondering what I'm missing with header() to force the file through the browser.
Missing this:
header("Content-Type: application/force-download");

Why is this PHP download script failing only on Android

I have a simple script that allows someone to download a movie file to their device. The code works well on everything I've tested except for Andriod. The Android device butchers the name and the file extension. It might call the file 2.qt or .bin. Why is this failing?
<?php
if(isset($_GET['filename'])) {
header('Content-Description: File Transfer');
header('Content-Type: movie/quicktime');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize("$file"));
ob_clean();
flush();
readfile("$file");
} else {
echo "Link: <a href='test.php?filename=test.mov'>Download Video</a>";
}
?>
Because Android doesn't natively support Quicktime, an Apple technology. It's also very possible that the client used to download this app isn't respecting the filename set on the http envelope as the name it uses to write the file to the filesystem, as there is nothing forcing it to.
The problem was that the filenames had spaces, and Android doesn't like that. I removed the spaces and everything is fine.

Categories