Previously I want to output a doc file it is fine.
But when I change to docx then it told me can't open docx because found a problem with its contents/the file is corrupt.
If I take out 'echo', then no error occurs but with empty content.
Here is my code:
$fileName = $m_year . "-" . $m_month . "-" . $m_con_id . " - Monthly Report.docx";
header("Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("content-disposition: attachment;filename=\"". $fileName ."\"");
header('Cache-Control: public');
$content = ets_docMonthlyReportTemplate();
echo $content;
Then I change to vsword, but give me illegal name character. Location: Part:/word/document.xml
require_once '/vsword/VsWord.php';
VsWord::autoLoad();
$doc = new VsWord();
$parser = new HtmlParser($doc);
$content = ets_docMonthlyReportTemplate();
$html= $content;
$parser->parse($html);
echo '<pre>'.($doc->getDocument()->getBody()->look()).'</pre>';
$doc->saveAs($fileName);
Can I just output by header MIME Type?or must apply library?
Thanks.
You need to use a library to accomplish this.
One of the more popular ones is PHPWord
There is also VSword
There are also paid soluitions PHPDocx
readfile($fileName); should solve it
`require_once '/vsword/VsWord.php';
VsWord::autoLoad();
$doc = new VsWord();
$parser = new HtmlParser($doc);
$content = ets_docMonthlyReportTemplate();
$html= $content;
$parser->parse($html);
echo '<pre>'.($doc->getDocument()->getBody()->look()).'</pre>';
$doc->saveAs($fileName);
header("Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("content-disposition: attachment;filename=\"". $fileName ."\"");
readfile($fileName);`
Related
I'm using PHPSpreadsheet to export an html table to .xlsx file.
Everything works fine except...
I want to give the filename the greek fullname of the employee that is stored in a SESSION but I'm getting something like this:
2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx
Is there a way I can set the actual name as the file name???
Edit: the code is
if(isset($_POST["file_content"]))
{
$temporary_html_file = './tmp_html/' . time() . '.html';
file_put_contents($temporary_html_file, $_POST["file_content"]);
$reader = IOFactory::createReader('Html');
$spreadsheet = $reader->load($temporary_html_file);
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$filename = $ecoYear . "_" . $_SESSION['fullname'] . '.xlsx';
$writer->save($filename);
header('Content-Type: application/x-www-form-urlencoded');
header('Content-Transfer-Encoding: Binary');
header("Content-disposition: attachment; filename=\"".$filename."\"");
readfile($filename);
unlink($temporary_html_file);
unlink($filename);
exit;
}
html_entity_decode — Convert HTML entities to their corresponding characters
<?php
$orig = "2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx";
$text = html_entity_decode($orig);
echo $orig . PHP_EOL;
echo $text . PHP_EOL;
?>
Output: 72103715.php
2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx
2021_ΑΘΑΝΑΣΙΟΣ ΛΑΜΠΡΙΔΗΣ.xlsx
I'm aplying this link in order to develop an easy script to download an Android .apk file. But instead of download file, is downloading itself.
https://stackoverflow.com/a/22605604
Here is my code:
<?php
$filename = shell_exec('ls /var/www/html/app/*.apk');
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile($filename);
exit();
?>
Php.info is rendered correctly by Apache.
Thanks.
Your path is not set correctly!
Try to use a real path/filename:
$filename = "/var/www/html/app/nameofyourfile.apk";
And also your content-type is not set correctly, try:
Content-type: application/com.android.package-install
If these two errors are corrected, it would probably force to download your apk file.
Anyway, this should work ->
<?php
$filename = "/var/www/html/app/yourfile.apk";
$contenttype = "application/com.android.package-install";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=$filename);
readfile($filename);
?>
I know that there are a ton of questions about this here but every answer i tried didn't fixed my problem
Problem
I have xml saved on a string and i want so save it to a file but don't want the xml to be in a single line i want to write to the file in pritty print
Also there is a bigger problem with this the instead of the this " it shows this " since this file will be loaded in a diferent website i can't have "
Note that the $content variable has " not "
I know that \n doesn't matter but to make it easier to analize the generated xml file i need the xml to be formated
this is what i am using
$dirname = "temp/" . uniqid();
mkdir($dirname);
$filename = $dirname . "/gen.xml";
$doc = new DomDocument($content);
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->save($filename);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=gen.xml");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
readfile($filename);
$fh = fopen($filename, 'a');
fclose($fh);
unlink($filename);
rmdir($dirname);
I also tried
header("Content-type: text/xml");
and
file_put_contents
DomDocument's constructor does not accept an xml string - rather, an optional version and encoding.
What you probably want is to load your $content xml string instead:
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$doc->loadXML($content);
$doc->save($filename);
I have text file contains Sample of CSV file format, I want my users can download that file on a link click.
This file resides in this folder stucture:
assets->csv->Sample-CSV-Format.txt
This is the code that I have tried to far:
<?php
$file_name = "Sample-CSV-Format.txt";
// extracting the extension:
$ext = substr($file_name, strpos($file_name,'.') + 1);
header('Content-disposition: attachment; filename=' . $file_name);
if (strtolower($ext) == "txt") {
// works for txt only
header('Content-type: text/plain');
} else {
// works for all
header('Content-type: application/' . $ext);extensions except txt
}
readfile($decrypted_file_path);
?>
<p class="text-center">Download the Sample file HERE It has a sample of one entry</p>
This code is downloading the file on page load instead of link click. Also, it is downloading the whole html structure of the page I want only the text what I have written in text file.
Please guide where is the issue?
You can do this simply in by HTML5 download atrribute . Just add this line in your downloading link .
HERE
You can do it like this, it won't redirect you and also works good for larger files.
In your controller "Controller.php"
function downloadFile(){
$yourFile = "Sample-CSV-Format.txt";
$file = #fopen($yourFile, "rb");
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=TheNameYouWant.txt');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($yourFile));
while (!feof($file)) {
print(#fread($file, 1024 * 8));
ob_flush();
flush();
}
}
In your view "view.php"
Download
make it like this
someother_file.php
<?php
$file_name = "Sample-CSV-Format.txt";
// extracting the extension:
$ext = substr($file_name, strpos($file_name,'.')+1);
header('Content-disposition: attachment; filename='.$file_name);
if(strtolower($ext) == "txt")
{
header('Content-type: text/plain'); // works for txt only
}
else
{
header('Content-type: application/'.$ext); // works for all extensions except txt
}
readfile($decrypted_file_path);
?>
some_html_page.html
<p class="text-center">Download the Sample file HERE It has a sample of one entry</p>
To my view its better to have the download code to the client side, than to have a controller-method written for this.
you can use this ref
public function getTxt()
{
$this->load->helper('download');
$dataFile = "NOTE87";
$dataContent = array();
$dt = "Date :23/07/2021";
$dataContent= array(
"\n",
"\t\t\tUTI AMC Limited\n",
"\t\tDepartment of Fund Accounts\n",
"\n",
"\tReissue of Non Sale Remittance - Axis Bank Cases\n",
"\n",
"\t\t\t\tDate :".$dt."\n",
"\n",
);
force_download($dataFile,implode($dataContent));
}
I need some eduction please.
At the end of each month, I want to download some data from my webserver to my local PC.
So, I've written a little script for that, which selects the data from the DB.
Next, I want to download it.
I've tried this:
$file=$month . '.txt';
$handle=fopen($file, "w");
header("Content-Type: application/text");
header("Content-Disposition: attachment, filename=" . $month . '.txt');
while ($row=mysql_fetch_array($res))
{
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
If I run this, then the file is created, but my file doesn't contain the data that I want. Instead I get a dump from the HTML-file in my browser..
What am I doing wrong..
Thanks,
Xpoes
Below script will help you download the file created
//Below is where you create particular month's text file
$file=$month . '.txt';
$handle=fopen($file, "w");
while ($row=mysql_fetch_array($res)){
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
//Now the file is ready with data from database
//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;
Your current code does not output a file, it just sends headers.
in order for your script to work add the following code after your fclose statement.
$data = file_get_contents($file);
echo $data;