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
Related
I am trying to export few data in CSV file. Though the file is successfully written but it is not downloading instead is being read by browser. $csvarray is data array and fputcsv2 is function similar to PHP default fputcsv function
$output = fopen("php://output",'w') or die("Can't open php://output");
rewind( $output );
foreach($csvarray as $product) {
fputcsv2($output, $product,',',' ');
}
header("Content-type: text/csv");
header('Content-Disposition: attachment; filename="export.csv"');
readfile ($output);
exit();
Finally the issue got resolved, may be helpful for somebody,
when I generate the data array before dumping it into the CSV file I printed array using $print_r with <pre> ... </pre> tag. though I remove $print_r but <pre> ... </pre> tag was still there preventing file to download. As soon as I remove this tag file is now downloaded.
First example in the readfile() documentation should help you: http://php.net/manual/en/function.readfile.php
Example #1 Forcing a download using readfile()
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
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();
In my code I am creating a file with fgetcsv. I download it and open the file but all the html from my page is included with the csv file. Is there anyway I can just have the data I want to output from the file and not the extra html. my function for creating the csv is below
function makefile()
{
header('Content-type: text/csv');
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment;filename=file.csv");
$this->filepointer = fopen('php://output', 'a');
for($count=0;$count < count($this->alldata);$count++)
{
fputcsv($this->filepointer, $this->alldata[$count]);
}
fclose($this->filepointer);
}
also I was wondering if i use $this->filepointer = fopen('file.csv', 'w') will the file still be populated with the html. as I dont have to have the file downloading I was just using it to check if the file was being created in the correct format. thanks again
You can try the below code. You need to add read_file at the end after setting some header as below.
<?php
if(isset($_GET['link']))
{
$var_1 = $_GET['link'];
$file = $var_1;
if (file_exists($file))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
} //- the missing closing brace
?>
use ob_end_clean() ; before writing the file
I search web for a function/command in php/javascript/xul which can transfer a file to local directory from a http server, but didn't find any sufficient answers.
Is there any function/command avaliable to do this job, using any of PHP/Javascript/XUL?
Here is the php script for file download:
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
http://php.net/manual/en/book.ftp.php, here is a good example http://www.php.net/manual/en/ftp.examples-basic.php
Try this function http://www.php.net/manual/en/function.ftp-get.php
This cannot be done with javascript, but in PHP you have to send the proper headers and then serve the file like this:
Example: Force download of CSV file
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename=example.csv');
header('Pragma: no-cache');
readfile("/path/to/yourfile.csv");
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);