how to download html file in php - php

I'm creating a doc file with the php.
I'm using echo to output everything but I can see that my html is properly created in my response but the download is not starting.
How can I make this download to start?
Part of my code:
header('Content-Description: File Transfer');
header("Content-type: application/msword; charset=utf-8");
header("Content-Disposition: attachment;Filename=test.doc");
echo $main_html; //html code

I tested this, and it works perfect.
file_put_contents("test.doc",$main_html);
header('Content-Description: File Transfer');
header("Content-type: application/msword; charset=utf-8");
header("Content-Disposition: attachment;Filename=test.doc");
readfile("test.doc");
unlink("test.doc");
die;
Note : if you may already know, header only works before passing any output to the browser, including line break and even a space .

Use this:
header("Content-Type: text/html");
header("Content-Disposition: attachment; filename=read.html;");
header("Content-Transfer-Encoding: binary");
echo $html;

Your code works fine for me.. I ran this on my server
<?php
$main_html = "<h1>Hey I'm Nate</h1>"; // I did put this in
header('Content-Description: File Transfer');
header("Content-type: application/msword; charset=utf-8");
header("Content-Disposition: attachment;Filename=test.doc");
echo $main_html; //html code
Did you define $main_html before echoing? Because all I changed was added some html to it.
It started a download right away, with the correct content in the document.

To start an download, you have to use the content-type application/octet-stream to start an download as mentioned in RFC 2046.
Btw. you have to echo the content of the doc file and not your html content(i.e. use readfile('test.doc') as mentioned in other answers or simply echo the content of your doc file)

Related

docx file is corrupted after download from server using php

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;

how to download a .txt file by clicking using PHP

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);
}

Including source file name on readfile?

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!

Download automatically a file(.sql) in wwwroot using PHP

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');

Download file corrupt. Content-type not working?

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

Categories