How to download files from db in codeigniter - codeigniter

I am creating a resource site for my campus where teachers upload files to folder and reference of that filed stored in db, and students can download those files using download option in student view side, and all these I have done in codeigniter using model view controller.
I need help for coding to download files. I managed to display a set of files but could not download yet, can anyone please help me with this? I'll be so thankful.

Try this
$download_file = 'C:\example.zip';//path of the file
$content_type = mime_content_type($download_file);//mime type of the file
$file_name = basename($download_file);//file name
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: ".$content_type);
header("Content-Disposition: attachment; filename=\"".$file_name."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($download_file));
while (ob_get_level()) {
ob_end_clean();
}
readfile($download_file);

Related

How to force download a file in Joomla 3?

Wrote a custom module for Joomla. The user enters a file name in the text filed and the pdf file with the entered value as name should be downloaded. But when clicked the page is redirected to home page. Here's the code
$file = $_POST['posttext'] . '.' . 'pdf';
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;
}
Tried adding ob_flush() as one of the asker suggested but no good. The code works perfectly as a separate php file.
Try the following
$app = JFactory::getApplication();
$file = $app->input->get('posttext') . '.pdf';
if (JFile::exists($file)) {
// File headers
header("Content-type: application/pdf");
header("Content-Disposition: attachment");
// File contents.
readfile($file);
}
If you are still having issues, you could check if the file exists in Joomla and download the file in a new tab/window using window.open()

Codeigniter Flash data not getting cleared

public function student_email()
{
$opt=$this->uri->segment(3);
$class=$this->input->post('class');
$edu_level=$this->input->post('edu_level');
$data=$this->newsletter_model->get_StudentData($opt,$class,$edu_level);
$subject=$this->input->post('txtSubjectRequired');
$message=$this->input->post('txtMessageRequired');
$message=nl2br($message);
$note="student";//Variable for Identifying Students alone
//Test Email
$email=$this->input->post('txtEmail');
if(trim($email)!=''){
$note="test";
$name="User";
$data=$email;
$this->sendMail($data,$subject,$message,$note,$name);
}
else {
//$this->sendMail($data,$subject,$message,$note,$text,$name='');
}
if($this->session->userdata('mail_status')=="success"){
$this->session->set_flashdata('success', 'Mail sent Successfully.');
}
else{
$this->session->set_flashdata('success', 'OOPS.! Mail Sending failed..!.');
}
$this->session->unset_userdata('mail_status');
redirect('/newsletter/student/'.$opt , 'refresh');
/**Note:
* Filter by field of study and Last topic need to be coded..
*/
}
When click the back button, flash data not getting cleared.
is it a cache problem..? any other
any solution instead of disabling cache?
Please help me with the solution..
<?php
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
?>
I found this code
It solves the problem..

Why jqgrid can't get json data from joomla?

I write codes for using jqgrid(version 4.4.4) in joomla(version 2.5.9).this is my codes I found the jqgrid cann't display data from joomla json_encode.but no in joomla is OK.
if jqgrid url:testjson.php is ok.
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Content-Type: application/json;charset=utf-8");
header('Content-Disposition,attachment;filename=”json.json”');
$response=new stdClass();
$response->page = 1;
$response->total = 1;
$response->records = 1;
$response->rows[0]['id']='1';
$response->rows[0]['cell']=array("1","vlan");
echo json_encode($response);
?
I get json:{"page":1,"total":1,"records":1,"rows":[{"id":"1","cell":["1","vlan"]}]},and jqgrid work fine.
When jqgrid url:index.php?option=com_sysconfig&view=vlan&task=ajaxvlane.getvlanlist&format=raw
In sub-controller,in function getvlanlist,this codes is blow:
<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header("Content-Type: application/json;charset=utf-8");
header('Content-Disposition,attachment;filename=”json.json”');
$response=new stdClass();
$response->page = 1;
$response->total = 1;
$response->records = 1;
$response->rows[0]['id']='1';
$response->rows[0]['cell']=array("1","vlan");
echo json_encode($response);
JFactory::getApplication()->close();
?>
Through firebug i can see the json:{"page":1,"total":1,"records":1,"rows":[{"id":"1","cell":["1","vlan"]}]} same as testjson.php result.
but jqgrid can't show any data.
What error causes a problem?thank you.
BTW,if I use joomla creating xml data ,jqgrid work fine.
I get the answer:because the code editor is UTF8 BOM charaset.If use no BOM uft-8,then it work fine.

How to send a file to browser for downloading?

When client request for a file, I use this code to send it:
public static Result download(String file) {
File file = getRealFile(file);
return Ok(file);
}
But I found the browser will not download it, but display its content instead. The response header:
Content-Type text/plain
Transfer-Encoding chunked
What's the correct way to send a file?
Update
Per Razvi's answer, I found an answer seems good for this question: https://stackoverflow.com/a/1074925/342235
But do we really have to set so many headers?
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filepath");
header("Content-Type: mime/type");
header("Content-Transfer-Encoding: binary");
// UPDATE: Add the below line to show file size during download.
header('Content-Length: ' . filesize($filepath));
You need to set the Content-Disposition header. I believe the value of the header should be attachment. See Manipulating the response doc for this.
For play 2.0 the following works without explicitly setting the header:
ok(new FileInputStream(file))

Open PDF in a new tab using dompdf

Im trying to generate pdf using dompdf, how can I open the pdf in a new tab in a browser? Like, I Click A link for the PDF and it should open in a new tab, not save it automatically. I want to give the user a choice to save the file after seeing it first. how do i do that?
whenever i use $pdf->output at the end of the file, it does not change a thing, the file is still downloaded automatically.
please help. thanks.
Whether a PDF is downloaded or viewed in the browser depends on a couple of factors. One is your browser settings, but we have no control there. The second is how dompdf present the PDF to the browser. You can tell dompdf to offer the PDF for direct viewing using $dompdf->stream('my.pdf',array('Attachment'=>0));.
As far as opening in a new tab. That depends on how you are generating the PDF. But the simplest way it to provide a link with a target attribute.
I have a same problem into my site (http://www.pdfwebcreator.com)
My solution is:
$myfile = fopen($strFileName, "r") or die("Unable to open file!");
$fileSize = filesize($strFileName);
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"temporaryPdf.pdf\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fileSize);
echo fread($myfile, $fileSize);
}
I don't know if you got, but if you use false in this line:
$dompdf-> stream("pasta/doc/relatorio.pdf", array("Attachment" => false));
You can see the pdf in the browser.
Well that you can do with the ->stream(); at the end of the chain.
Example:
//Routes (web.php in case laravel version >= 5.4)
Route::get('/pdf', 'PdfController#pdfStream')->name('pdfStream');
//PdfController.php
public function pdfStream(Request $request) {
$data["info"] = "I is usefull!";
$pdf = PDF::loadView('whateveryourviewname', $data);
return $pdf->stream('whateveryourviewname.pdf');
}
//yourViewPage.blade.php
<a href="{{route("pdfStream")}}" target="_blank" > click me to pdf </a>
Here more information
Am still experimenting, but something like this works fine as well
$pdf->loadView('file/path', compact('values'));
return $pdf->stream();
With this you can add dynamic values to your pdf file page within the browser.

Resources