I'm having a bad time with the readfile function on php. I'm setting up a web file server, but many of the users have problems with the downloads.
Basically, I'm using Chrome on my computer and everything works fine. Some of the users have problems on Android browser: they get a .bin file instead og .doc, or get a correct .doc file, but they can't open it. Also, someone gets a corrupted .doc file on the computer.
I have read many questions on this site but they weren't so helpful.
Since I have tried many solutions, I will post a shortened code snippet to give an idea of what I am trying to accomplish. I am using apache2, couldn't it be a configuration problem?
Does someone have my problems too?
<?php
//here I take the user, the pwd and the file RELATIVE path
//there are no errors here
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="' . $name . '"');
readfile($file);
?>
EDIT: full code
<?php
session_start();
$data = $_SESSION['data'];
$name = $_GET['name'];
if (isset($data)) {
//private file
$file = "path/".$data."/".$name;
} else {
//public file
$file = "path/public/".$name;
}
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $name . "\"");
readfile($file);
?>
Related
I am trying to force download a file in my php program under a new name (new_name). I am changing the file name but keeping the extension the same as roginal file. This is part of my code:
$original_filename = "attachments/20180315.log";
$new_filename = "new_name." . pathinfo($original_filename, PATHINFO_EXTENSION);
header("Content-Length: " . filesize($original_filename));
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . $new_filename . '"');
header('Pragma: no-cache');
readfile($original_filename);
exit;
My download is working fine when I am downloading a text file (i.e. in example above 20180315.log is a text file). When I try to do the same with a binary file (such as pdf) I am getting an empty new_name.pdf.
Any suggestion?
I have always found the exact headers required to be a bit messy and dependent on the browser, there are a lot of options, and you will see a lot of variations posted, so i suggest the following, but you may need to just test a number of combinations until you get it right, and i suggest making sure you test it on as many browsers as you can.
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . $new_filename . "\"");
readfile($original_filename);
I am trying to download a file. I am using IE11. I have tried several methods to do this. Currently I am trying to use the header with Content-Disposition method. I have tried to do this a few different ways according to other answers people have given. And it does download. But instead of downloading the file I point it to, it downloads the file it is written in. So if I tell it to download example.txt in my test.php file. It will only download test.php.
These are the methods I have tried:
This one is written in test.html:
<?php
$filename = "example.txt"
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
?>
I've also tried making it a button:
BUTTON
Where download.php is:
<?php
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename="'.$file.'"');
?>
I tried this:
<?php
header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="example.txt"');
header("Content-Length: " . filesize("example.txt"));
$fp = fopen("example.txt", "r");
fpassthru($fp);
fclose($fp);
?>
And this:
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");
There are many more slight variations and mix and matching that I have tried. All have the same problem that the .html or .php file downloads rather than example.txt. Does anyone know why this would happen? Is something not supported in IE11? I do not think it is a syntax error simply because most of these I copied from other answers online. I have tried with example.txt existing and not existing, in this folder and other folders.
EDIT: So it turns out that these all work, I was just using them wrong. I had been trying to make isolated files to run this code so I could test it without interference from the rest of the functions on my website, but this left the php files without the resources they needed to actually run properly. When I put the code into the actual files on the website it worked perfectly. smh
Try this :
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($File));
header('Content-Disposition: attachment; filename=' . basename($File));
readfile($File);
exit;
So, I need a little help here. I have a site which hosts some mp3s. When users click on the download url, it links directly to a file called downloadmp3.php, which goes 2 parameters in the url...the php file is included below, and it's basically supposed to FORCE the user to save the mp3. (not play it in the browser or anything).
That doesnt happen. Instead, it seems like the file is WRITTEN out in ascii to the browser. It seems like it's the actual mp3 file written out.
Here is my downloadmp3.php file...please, what's wrong in this code.
It works on my local LAMP (Bitnami Wampstack on windows)....that is, on my local testing environment, it sends the file to my broswer, and I can save it. When I upload it to the real server, it basically writes out the mp3 file.
Here is the culprit file, downloadmp3.php...please help
<?php
include 'ngp.php';
$file = $_GET['songurl'];
$songid = $_GET['songid'];
increasedownloadcount($songid);
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
ob_clean();
flush();
readfile($file);
exit;
}
?>
By the way, this site only hosts mp3s - no other audio or file format. So, this downloadmp3.php script should ideally ask the user where they want to save this file.
Thanks for your help in advance.
I think the filename should be in quotes:
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
Change the content-type value to text/plain. With this browser wont recognize it and wont play the file. Instead it will download the file at clients machine.
Seems there is too many headers. I am sure they do SOMETHING... but this code works.
This code works with MP3 files.... downloads to a file. Plays without a problem.
if(isset($_GET['file'])){
$file = $_GET['file'];
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename=".$file.'"');
readfile('path/to/your/'.$file);
exit();
}
You can access it with ajax call, or this:
<a id="dl_link" href="download.php?file=<>file-you-wish-to-download<>" target="_blank">Download this file</a>
Hopefully this is of some use
I'm currently using localhost to run my pages and currently I am trying to download the files my users have uploaded and stored in a folder called uploaded_files
this is the code for my download page which isn't working and I'm not quite sure what's wrong.
<?php
$file = $_GET['file'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\";");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile("C:\xampp\htdocs\FYPproject\uploaded_files/".$file); //showing the path to the server where the file is to be download
exit;
} else {
echo "Download failed";
echo $file;
}
?>
file_exists() need to receive the path of the file, if the file name is "test" and it's in "uploaded_files/" so you need to check file_exists("uploaded_files/test"). You don't need to pass the whole path if your php is already in "FYPproject" folder. It's the same for downloading files, you don't need to pass the whole path for readfile().
You are passing a file path with backward slashes and forward slashes to readfile. Even if this is not the cause of your problem you should change it. Change it to this:
readfile("C:\xampp\htdocs\FYPproject\uploaded_files\".$file);
Not all browsers support application/force-download so in this case try replacing the above code with application/octet-stream and see if it works
I know this question has been asked, but the solutions offered are not working for me. I am able to download PDFs, but Adobe Reader is telling me it's either corrupt or decoded improperly. Does anyone have an alternative solution, or possible fix?
$path = "http://www.laerdalmail.com/discoversimulation/downloads/needs_assessment_form.pdf";
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=needs_assessment_form.pdf");
readfile($path);
exit;
readfile should be given a filesystem path, not a URL. something like this:
$path = '/websitefolder/discoversimulation/downloads/needs_assessment_form.pdff';
// or
$parh = 'c:\something\needs_assessment_form.pdff';
Try adding in content length too:
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));