Or possible for users to download the file without saving it on the server?
I am getting data from the database, and I want to save them .doc (MS Word) file.
if(isset($_POST['save']))
{
$file = "new_file2.doc";
$stringData = "Text text text text...."; //This data put in new Word file
$fh = fopen($file, 'w');
fwrite($fh, $stringData);
fclose($fh);
header('Content-Description: File Transfer');
header('Content-type: application/msword');
header('Content-Disposition: attachment; filename="'.$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);
unlink($file);
exit;
}
How should the code look like without this:
" $fh = fopen($file, 'w');
fwrite($fh, $stringData);
fclose($fh);"
and this "unlink($file);"
I hope to understand what I need
enter code here
You just need to output headers and then the content:
header(...);
echo $stringData;
No need to play with any files.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment;
filename=\"$_fileName\"");
ob_clean();
readfile($_filePath);
Related
I want to send a file directly to browser:
<?php
#ini_set('error_reporting', 0);
#apache_setenv('no-gzip', 1);
#ini_set('zlib.output_compression', 'Off');
#ob_implicit_flush();
$file = 'ORG.iso';
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\\'));
$file_size = filesize($file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quoted);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
#ob_end_flush();
set_time_limit(0);
$f = fopen($file, 'r');
while (!feof($f))
{
$buff = fread($f, 204800);
echo $buff;
sleep(1);
}
fclose($f);
It shows correct file size in Firefox and Chrome right after visitor enters the link. However it does not work in jDownloader. It seems like jDownloader is waiting for the whole script to end.
When I try a normal, static file from the same webserver- jDownloader shows file size instantly.
So obviously something is wrong with my script.
I am having a problem that I just can't solve and I'm going madness with this!
Basically I have to export a csv file. The site is obviously running on a server, and the idea is that onclick the file is generated and automatically downloaded.
Everything is just fine, however, the file is being download to the server directory instead of the local user machine. I just can't make it.
What is wrong? Thank you very much!
if(!isset($crm_contas))
$crm_contas = new crm_contas;
$resultGetValues = $crm_contas->getExportContas($filtros, $idsToSend);
// filename for download
$filename = "ContasExportadas" . date('Ymd') . ".csv";
$output = fopen($filename, 'w');
foreach($resultGetValues as $row) {
fputcsv($output, $row);
}
fclose($output);
$filename = realpath($filename);
$fp = #fopen($filename, 'rb');
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".filesize($filename));
} else {
header('Content-Type: "application/octet-stream"');
header('Content-Disposition: attachment; filename='.$filename);
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".filesize($filename));
}
fpassthru($fp);
fclose($fp);`
--> This is the action that calls the code above.
echo "<a href='#' onclick='changeFormAction(\"form_list\",\"".$CONF['HOME']."/contas/index.php?view=export\");document.form_list.submit();changeFormAction(\"form_list\",pesquisa);'><img src='".$CONF['HOME']."/images/structure/excel.png' alt='Exportar' title='Exportar'></a>";
Meanwhile, I noticed that error_log is writting down this lines when I try do download.
[21-Apr-2016 16:23:44 UTC] PHP Warning: Module 'imap' already loaded in Unknown on line 0
I am a new PHP programmer. I am trying to write code to allow users to download text file from my website. I followed the answers to similar questions on this subject and put together this following test program. It did not force the download to go to a file, instead, it sent the content to the screen (in Chrome, IE, Firefox). Could someone point out what I did wrong?
Here is my test code:
<?php
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=\"test.txt\"");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
exit();
?>
You can simply use
$handle = fopen("file.txt", "w");
fwrite($handle, "Shadow ");
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
Edited , try it on single php page .
This code will display the text file content in the screen, and user can download it as a text plain file:
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
echo file_get_contents($file);
But this code (by #Drop Shadow) will force browser to show download dialog without displaying anything:
$file = "test.txt";
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
$type = filetype($file);
// Send file headers
header("Content-type: $type");
header("Content-Disposition: attachment;filename=\"test.txt\"");
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header('Expires: 0');
// Send the file contents.
set_time_limit(0);
readfile($file);
exit();
I am trying to send a PDF file. Here is the code that is executing:
$file_path = '/path/to/file/filename.pdf'
$file_name = 'filename.pdf'
header("X-Sendfile: $file_path");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename='$filename'");
readfile($file_path):
whenever I download the file directly, it is fine. however when I try to download the file via this script, a file downloads that cannot be opened. my pdf reader tells me it cannot open a file of type 'text/plain'. I also tried setting the Content-type to application/pdf , but I get the same errors. What am I doing wrong here?
Have you tried this?
$file = '/path/to/file/filename.pdf';
header('Content-Disposition: attachment; filename="'. basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
Using readfile() also eliminates any memory issues you might encounter.
try the code below.
header("Content-Type: application/octet-stream");
$file = "filename.pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp)
If the above one does not help you try the below
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(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);
exit;
Don't for get to set the file path $file_path as required.
Im using a button to direct to a script which creates a text file:
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
?>
after that I want the web browser to propose to download or open this file
how should I do? Thanks
use readfile() and application/octet-stream headers
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
?>
$content = file_get_contents ($filename);
header ('Content-Type: application/octet-stream');
echo $content;
Should work