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);
}
Related
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'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)
I have a problem about downloading Excel file from server.
the excel file was already saved on the server and I downloaded it using the code below.
if(file_exists($reportPath)){
//content type
header('Content-type: application/vnd.ms-excel');
//open/save dialog box
header('Content-Disposition: attachment; filename='.$dirFile[count($dirFile)-1]);
//read from server and write to buffer
readfile($reportPath);
}
But the downloaded file was corrupted.
I'm pretty sure that the file saved on the server is not corrupted since I have get it manually from the server to my local desktop.
Meaning, the data has been corrupted on the fly.
Please help, thank you, I'm using PHP
Can you try these headers?
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$dirFile[count($dirFile)-1].'"');
header('Cache-Control: max-age=0');
And see if it's working... Cheers!
Download script should be separate file. Actually you should not print out anything in this script
//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;
$fileName = "data.xls";
$object_writer = PHPExcel_IOFactory::createWriter($object, 'Excel5');
ob_end_clean();
header("Content-Type: application/download");
header('Content-Disposition: attachment;filename=' . $fileName);
$object_writer->save('php://output');
use ob_end_clean() to clear the output buffer.
This is what fixed my issue :
adding ob_end_clean funct after the save.
adding exit at the end of the script.
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');