I'm trying to download a a word document (.docx) from the server using php. Unfortunately the document I get is corrupted. I can open the document with word, but I get these annoying messages (File is corrupted etc.). Here is my code:
$file = "documents/".$_POST["id_form"]."_document.docx";
$filename = $_POST["id_form"]."_document.docx";
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=".$filename);
readfile($file);
Thanks for your help!
UPDATE SOLUTION
I got the solution. I had to put ob_end_clean(); before the header and a exit; after readfile($file). Now it works fine.
Here is the working code:
$file = "documents/".$_POST["id_form"]."_document.docx";
$filename = $_POST["id_form"]."_document.docx";
ob_end_clean();
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=".$filename);
readfile($file);
exit;
I have a .TXT file on my xampp server, if a user clicks on a link it should download, but instead it opens up in my browser window.
My code looks as follows:
<?php echo $files->task_verify_file;?>
any one solve the problem thanks
I did it like this:
if (file_exists ($file)) {
header("Content-Type: text/plain; charset=utf-8");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename);
readfile($file);
}
I have a very simple hide-download-path-script setup like this:
On index.html I have this link:
save
On savefile.php I have this bit of code:
$file = 'http://www.mysite.com/files/correct_horse_battery_staple.rar';
header("Content-Type: application/force-download");
#readfile($file);
This does seems to work, but unfortunately it downloads the file as savefile.php rather than correct_horse_battery_staple.rar.
Is there any way to change not only the file name but also the extension?
I have had same problem
Solved as below:
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($filename);
I hope it help u
Your link goes to savefile.php, and the browser never got another filename than savefile.php.
You need to add a header like:
header('Content-Disposition: attachment; filename="correct_horse_battery_staple.rar"');
or better...
header('Content-Disposition: attachment; filename="'.basename($file).'"');
Hope it helps!
So far here what i've tried it can download the sql file but it is empty
//test.php
<?php
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=wordpress_db1.sql');
?>
Here is what my root folder look like
I want to download the wordpress_db1.sql file when I run the test.php but I always get empty on it. How can I fix this? thanks!
Below code will do the trick for you.
<?php
$file_name = 'file.sql';
$file_url = 'http://www.example.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
?>
What have you gone wrong is readfile($file_url);. Setting headers will not get the job done. you have use readfile($file_url);
Setting the headers doesn't read the file. You can name the file anything you want in the attachment. You have to actually emit the file:
readfile('wordpress_db1.sql');
I want to allow a user to download a pdf file, the download code is below....for some odd reason even though the file is being downloaded I get an error saying that the file has been damaged on the server...Could someone help me and point out where I am making my mistake.
<php
$name = $_POST["name_first"];
$mail = $_POST['email'];
$number = $_POST['phone_number'];
$email_message = "first name: {$name} email is {$mail} number is {$number} ";
mail('fanaa#gmail.com', 'Form Response', $email_message);
if ($mail == "" OR $name == "" OR $number == "")
{
echo "Enter valid details ";
}
else
{
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="tokina.pdf"');
readfile('docs/tokina.pdf');
}
?>
I used this code to download pdfs:
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile("$file");
}
This should be fine, and make sure there are no spaces or return characters (don't escape php at all is the best solution).
If you find your still having problems, open the corrupted file with notepad (there may be a php error warning inside).
Hope this helps!
Remove the headers and look at the page, do you see any error messages? If PHP outputs anything else than the actual PDF source, the file will appear to be corrupted.
header('Content-type: application/pdf');
enable PHP extension php_gettext and you are done.
try taking out the double quotes in
header('Content-type: "application/octet-stream"');
so it becomes
header('Content-type: application/octet-stream');
Maybe your content-type is not correct. try this one:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
Your PDF file tokina.pdf is either not uploaded or not in the same directory as the PHP file. That's why it's saving as "tokina.pdf.htm" - it's loading the HTML for a 404 page instead. That is why your browser/PDF viewer thinks the file is "corrupted" - because its extension is PDF but its contents are not.
Make sure the file is uploaded, and if it is, make sure readfile is pointing to the correct path. If it's not in the same folder, use a relative/absolute path, for example:
readfile('docs/tokina.pdf');
And yes, the content type should be application/pdf
Using this script
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filenamepath));
readfile($filenamepath);
I had the same problem. Comparing the original file and the downloaded file with a hexadecimal editor like UltraEdit, I found some characters at the beginning of the corrupted file.
The problem was that after ?> marking end of PHP code there were line terminators several times in my code.
Remove all the line terminators after ?> and read also the forum article Downloaded Files are corrupt - Common Problem. That worked for me.
I hope that can help you.
I use
$download_path = your path (where to look for the files)
set_time_limit(0);
$file_url = $download_path . $data['link'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_url). '"');
//then to read the file
readfile($file_url);
this usually works for me