I have a function that creates an HTML page and forces the download. Once the function is run, for some reason it is downloaded to the users computers twice. Here is my code
public function actionDownload($params)
{
// Get the article
$co_model=new Contentorder;
$co_model->load($params['id']);
$co=$co_model->getFields();
$file_name=str_replace(array(' ','.','&',',','\''),'',strtolower($co['article_title'])).".html";
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header('Content-type: text/html');
echo '<!DOCTYPE html><head><title>'.$co['article_title'].'</title></head><body>';
if ($co['id_contenttype']==3){ // Reusable
$lco_model=new Librarycontent;
$lco_model->load($co['id_librarycontent']);
$res=$lco_model->getFields();
if ($res['id_reusabletype']==3) // Youtube
{
echo $res['reusable_file'];
}
}
echo $co['article'].'</body></html>';
exit;
}
It successfully forces the download on the dynamically created HTML file, but it is downloaded twice for some reason when I only want the file to be downloaded once.
EDIT ------ Here are the headers
Related
I am new to CodeIgniter and was working on downloading a file. However, I want to download a file that resides on my local machine, I am able to locate the file by providing the path, also the file gets downloaded with a File type, However, I want my file to be in .csv format
Here goes my Controller's download function:
public function download()
{
$state_id=$this->input->post('state'); // gets me the state-id from viw's dropdown
$this->load->helper('download'); // Load download helper
$file="C:\\Users\usernew\\Desktop\\New folder\\".$state_id.".csv";
$filename=$state_id.'.csv';
if (file_exists($file))
{
$data = file_get_contents($file); //check file exists
force_download($fileName,$data);
}
else{
echo"not working!";
}
}
Where am I going wrong?
The force_download() takes in two parameters, i.e. the file name and the data to be written to that file. As the file already consists of some sample data, therefore, the function goes like this:
public function download()
{
$state_id=$this->input->post('state'); // gets me the state-id from viw's dropdown
$this->load->helper('download'); // Load download helper
$filename=$state_id.'.csv';
$file="C:\\Users\usernew\\Desktop\\New folder\\".$filename;
if (file_exists($file)) //check file exists
{
force_download($file,NULL); //NULL as the file already has data
}
else{
echo"not working!";
}
}
you can use header function instead force_download:
$file="C:\\Users\usernew\\Desktop\\New folder\\".$state_id.".csv";
$filename=$state_id.'.csv';
header("Content-type: text/csv");
header('Content-Disposition: attachment; filename='.$filename);
readfile( $file );
Hello fellow coders !
I am looking for a solution to show a html page while my php code prepares a .zip which is then downloaded. The reason is because sometimes the zips are quite bigger and take time to make.
The HTML page would be a basic "Please wait while your .zip files is being prepared".
The PHP side used is Symfony. So I come into my getInboxExportAction function by calling https://myapi.com/orders/orderid/inbox/export.
The download function (makeExportDownloadRequestResponse) works fine. But if I add a flush after making my $response, the .zip is printed to the html, instead of being downloaded...
public function getInboxExportAction(Request $request, $orderId)
{
$response = new Response($this->twig->render('base.html.twig',
['content' => '
<h1>Download</h1>
<p>Your zip is being prepared for download, please wait...</p>
']));
$response->headers->set('Content-Type', 'text/html');
//Here I would like to echo + flush my html.
//Then stop the flush and continue my code
$receivedOrder = $this->fetchRequestedReceivedOrder($orderId);
if (!$receivedOrder instanceof ReceivedOrder) {
return $receivedOrder;
}
if (!$receivedOrder->getSentorder()) {
$this->makeErrorResponse('Sent order was not found', Response::HTTP_BAD_REQUEST);
}
return $this->makeExportDownloadRequestResponse($receivedOrder->getSentorder(), $request->get('format'));
}
I am also very open to any other ideas anyone would have to fix this issue :)
Thanks,
Edit : My $this->makeExportDownloadRequestResponse() function returns a response, not a path. We unlink the file on the server for storage reasons.
$content = file_get_contents($zipTmpFile);
unlink($zipTmpFile);
$response = new Response($content);
$dispositionHeader = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'myFile_' . date('Y_m_d_h_i_s') . '.zip');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
Second edit : I understand that what I'm trying to do (change the content-type within one call) I generally frowned upon. I'm currently trying to think of a more elegant solution.
In your getInboxExportAction, just return a response with your text.
Then, in this response, add a <meta http-equiv="refresh" content="1;url=xxx"> tag to redirect the user to another action that will generate the zip file.
You can also handle this redirection with javascript.
Note that you can use a StreamedResponse to handle the zip download: https://symfony.com/doc/current/components/http_foundation.html#streaming-a-response
The most important thing is you set $response->headers->set('Content-Type', 'text/html');, the browser will not download the .zip file.
If your makeExportDownloadRequestResponse method can return the absolute .zip file path (or you can calculate it), you can try:
public function getInboxExportAction(Request $request, $orderId)
{
$response = new Response($this->twig->render('base.html.twig',
['content' => '
<h1>Download</h1>
<p>Your zip is being prepared for download, please wait...</p>
']));
$response->headers->set('Content-Type', 'text/html');
//Here I would like to echo + flush my html.
//Then stop the flush and continue my code
$receivedOrder = $this->fetchRequestedReceivedOrder($orderId);
if (!$receivedOrder instanceof ReceivedOrder) {
return $receivedOrder;
}
if (!$receivedOrder->getSentorder()) {
$this->makeErrorResponse('Sent order was not found', Response::HTTP_BAD_REQUEST);
}
// prepare the to be downloaded zip file
// then, do sth to get the .zip file path
$zipFilePath = $this->makeExportDownloadRequestResponse($receivedOrder->getSentorder(), $request->get('format'));
// re-set header
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=whateverName.zip');
header('Content-Length: ' . filesize($zipFilePath));
readfile($zipFilePath);
}
It will let browser downloads the zip file.
Finally, If your function is work as an API, and you need a frontend JS to execute it, try with Blob class on JS.
You can indeed use a StreamedResponse to get it rid of this issue. About the fact that the whole .wip file content being displayed because of the flush, try to call ob_flush before
I have a problem with calling PHPWord library in Code Igniter, I already using PHPExcel and there are no problems everything goes smoothly.
But I have a problem with this PHPWord, because when I tried to use the same method to call the PHPExcel, it doesnt solve anything. I still seeing an error.
It's not like I don't search it on the google, I already searched it, but still, no one can solve my problem, I don't where is the problem.
So let me explain what is the problem.
FYI: I using PHP 7.1.1
Because the PHP version, PHPWord got some errors that says a class named as a String that is known as variable, so because of that error, I changed it to Strings all of them that associated with that class.
After I changed it, I want to call the class with library file.
This is the file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'/third_party/PHPWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
use PhpOffice\PhpWord\Settings as Settings;
use PhpOffice\PhpWord\PhpWord as PhpWord;
/* I tried two different ways */
// class Word extends PhpWord {
// public function __construct() {
// parent::__construct();
// }
// }
Autoloader::register();
Settings::loadConfig();
And this is the Controller code that calls the class.
$this->load->library('word');
$section = $this->word->createSection(array('orientation'=>'landscape'));
After I did that, it says Non-existent class: Word, I don't know anymore where is the problem, is it because I changed the class name? or it doesn't compatible with my CI version(3)?
Because I already tried different ways and still cannot solve this problem, can someone help me with this problem and show me what I did wrong? Thank you.
I haven't tried with PHP7 but I found this Github repo which worked for me.
https://github.com/webeasystep/phpword_codeigniter
I just copied the application\third_party\PhpWord folder and application\library\PhpWord.php into the same locations in my project and then used some of the code from the controller provided, e.g...
public function test()
{
$this->load->library('Phpword');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getCompatibility()->setOoxmlVersion(14);
$phpWord->getCompatibility()->setOoxmlVersion(15);
$filename = 'test.docx';
// add style settings for the title and paragraph
$section = $phpWord->addSection();
$section->addText("Hello, world");
$section->addTextBreak(1);
$section->addText("It's cold outside.");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($filename);
// send results to browser to download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
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($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;
}
This works fine, I get a .docx when I hit my controller/test in a browser
Following library is working smoothly for me for PHP 7 version without any error.
https://github.com/rifqisucahyo/CI_PHPWord
class Word extends PHPWord {
public function __construct() {
parent::__construct();
$this->load->library('word');
}
function index() {
require_once APPPATH."/third_party/PHPWord.php";
$PHPWord = $this->word; // New Word Document
$section = $PHPWord->createSection(); // New portrait section
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);
$section->addText('Mohammad Rifqi Sucahyo.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
// Save File / Download (Download dialog, prompt user to save or simply open it)
$section->addText('Ini Adalah Demo PHPWord untuk CI', 'rStyle', 'pStyle');
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
}
}
I'm creating a custom PDF file containing some product details. I would like to display product attachments that are images. I have changed the attachment controller to
header('Content-Disposition: inline; filename="'.utf8_decode($a->file_name).'"');
When I visit the url through the browser it displays nicely:
http://domain.nl/nl/index.php?controller=attachment&id_attachment=483
But using this url inside an img tag in my PDF template gives:
TCPDF ERROR: [Image] Unable to get image: http://domain.nl/nl/index.php?controller=attachment&id_attachment=483
Does anyone have any idea on how to make this work?
I created a new file next to AttachmentController.php called AttachmentInlineController.php. Contents are slightly different:
<?php
class AttachmentInlineControllerCore extends FrontController
{
public function __construct()
{
$a = new Attachment(Tools::getValue('id_attachment'), $this->context->language->id);
if (!$a->id) {
Tools::redirect('index.php');
}
if (ob_get_level() && ob_get_length() > 0) {
ob_end_clean();
}
header('Content-Transfer-Encoding: binary');
header('Content-Type: '.$a->mime);
header('Content-Length: '.filesize(_PS_DOWNLOAD_DIR_.$a->file));
header('Content-Disposition: inline; filename="'.utf8_decode($a->file_name).'"');
#set_time_limit(0);
readfile(_PS_DOWNLOAD_DIR_.$a->file);
exit;
}
}?>
I removed the hook, changed content-Disposition to inline, and changed the function from postProcess() to __construct. Now if you use:
http://domain.nl/nl/index.php?controller=attachmentInline&id_attachment=483
It will display that attachment inline, and you can use this url in image tags etc.
I am using dompdf to create a pdf file out of an html file that gets created on-the-fly for the sole purpose of it serving as input for the pdf generator, however I am having trouble doing this, I implemented the code in this thread and everything works fine (I could output a simple pdf) however when I try to give it a more specific url I get this error:
An Error Was Encountered Unable to
load the requested file
here's the code that has the problem:
function printPDF(){
//write_file() usa un helper (file)
$this->load->library('table');
$this->load->plugin('to_pdf');
// page info here, db calls, etc.
$query = $this->db->get('producto');
$data['table'] = $this->table->generate($query);
$path_url = base_url().'print/existencias.html';
write_file($path_url, $data['table']);
$html = $this->load->view($path_url, 'consulta', true);
pdf_create($html, 'consulta');
}
Not sure about the exact problem, but please check this:
1) as stated in CI's manual, load->view's second parameter should be an associative array or an objet, translated to vars via extract. That may generate some problem generating $html.
2) try making $path_url relative to application/views directory, as read in CI's manual.
you should use tcpdf to create a pdf.
//create controller for example :
public function create_pdf()
{
$this->load->library("Pdf");
$data['results'] = // your data
$this->load->view('pdfview',$data);
}
//pdfview is the page having tcpdf code and your pdf code.
You can try it like this
public function export_pdf() {
$filename = 'FILENAME.pdf';
header("Content-Description: Advertise Report");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/pdf; ");
$file = fopen('php://output', 'w');
$header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
fputpdf($file, $header);
fputpdf($file, 'YOUR DATA');
fclose($file);
exit;
}