Unable to download pdf file from the server in codeigniter - codeigniter

I am using the following codes in my controller to download pdf files which are stored in a folder inside application folder in codeigniter.
In the view page I have a button, upon clicking it, it redirects to this controller and then it download the file. It downloads blank page(pdf).
Looking forward to any suggestion.
$filePath = APPPATH.'invoice/'."Let's Sunday#".$this->uri->segment(4).".pdf";
if(!empty($filePath) && file_exists($filePath)){
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" .$filePath);
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($filePath));
flush();
$fp = fopen($filePath, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush();
}
fclose($fp); }else{echo "file does not exist";}

Change the name in Content-Disposition to filename only instead of full path

Here is what I have in a download section within a CodeIgniter app:
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
flush();
exit;
}
The differences are in the ordering of the headers (which likely doesn't matter), the inclusion of the expires and pragma headers, and the Content-Disposition using the full basename.
The naming convention on that PDF filename might cause an issue...something else to test out.

Related

barryvdh / laravel-dompdf, Laravel 6, not response on Chrome, Android

I have this endpoint, which outputs (render in the browser) a PDF file:
$pdf = PDF::loadView('print.property', $data, []); // DOMPDF
$filename = 'test.pdf';
if($show) {
return $pdf->stream( $filename );
}
return $pdf->download($filename);
, where $show means render, but not download the PDF file.
For desktop everything works fine and the PDF file is rendered, but when I set with Chrome Dev Tools, the mobile simulator, the server doesn't response, but just stays in load mode.
I have tried with exit, without return with headers:
header("Content-Type: application/octet-stream");
header("Content-Disposition: inline; filename=\"$filename\"");
With Content-Disposition: attachment, it downloads the correct generated file.
The problem is somewhere in the headers I guess.
* I am using LiteServer.
This are some of the generated from the library response headers:
content-disposition: inline; filename="test.pdf"
content-type: application/pdf
I have tried and with this headers before: die ($pdf->stream( $filename));
header('Content-Length: 101840');
header('Content-Type: application/octet-stream');
or:
header('Content-Type: application/pdf');
or:
header('Content-Disposition: inline; filename="'.$filename.'"');
or:
header('Content-Disposition: attachment; filename="'.$filename.'"');
Nothing works. The closest that I ca get is downloading or render it as a string in the browser in Chrome (mobile).
personally I had problems with also. Now, I use "https://github.com/barryvdh/laravel-snappy" I do not know if on the v6 laravel it works.

How to read the attached file in http response using ruby?

I am new to ruby programming.
I have learning about the file attachments in ruby. In this I was learned about how to attach the file in http request. But I was searched about how read and save the attached file locally in http response. But I did not get anything for this.
UPDATE :-
From ruby script I do a http request to the php program.
The source for request is,
payload={"id":"1004"}
uri = URI.parse("https://example.com/test.php")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = payload.to_json
# request.use_ssl = true
# request.verify_mode = OpenSSL::SSL::VERIFY_NONE
# logger.info "request body is #{request.body.inspect}"
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE,:read_timeout => 25 ) { |http| http.request(request) }
From test.php, validated the id if it is valid then in response attached the file contents.
The source for this is,
$filename="test.mp3";
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($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));
ob_clean();
flush();
readfile($filename);
# exit;
}
What I want is, in the ruby script for the http request one response will be received from server. From that response I have extract the file contents and store it in locally.
I was searched about how to process the file contents in response. But I didn't get anything.
Kindly help me how to do this in ruby.

PHPExcel showing unreadable character

function districtexcelpdf(){
ob_start();
require_once('districtexcel.php');
$objPHPExcel = new PHPExceldis();
$objPHPExcel->districtExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="District_report.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_clean();
$objWriter->save('php://output');
exit();
}
In above code I got a unreadable character . Required help to solve this problem.
ob_clean(); at the start because it sending a junk value then after use ob_start(); and at the end ob_clean();

Why don't I receive an email using codeigniter email library when using local server?

I am using codeigniter's email library on mac os x apache 2 server. The weird thing I am running into is that when I use it on live server which is on hostgator.com the email goes through but when I run the same code on local apache2 server, I don't get any emails(even in spam box). What can be the problem? Here is the code
$this->load->library('email');
$this->email->from('your#example.com', 'Your Name');
$this->email->to('myemailhere#gmail.com');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
if($this->email->send())
{
var_export($this->email->print_debugger());
}
On both local and live server, this is var_dump data
'Your message has been successfully sent using the following protocol: mail
From: "Your Name"
Return-Path:
Reply-To: "your#example.com"
X-Sender: your#example.com
X-Mailer: CodeIgniter
X-Priority: 3 (Normal)
Message-ID: <5122a6ed242b8#example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
=?utf-8?Q?Email_Test?=
Testing the email class.
You can just try this.
Specify path for send mail in php.ini
sendmail_path = "path/to/php path/to/sendmail.php"
The second step - you can try to use this script
define('DIR','path/to/sendmail_dir');
$stream = '';
$fp = fopen('php://stdin','r');
while ($t = fread($fp,2048)) {
if ($t === chr(0)) {
break;
}
$stream .= $t;
}
fclose($fp);
$fp = fopen(mkname(),'w');
fwrite($fp,$stream);
fclose($fp);
function mkname($i = 0) {
$fn = DIR.date('Y-m-d_H-i-s_').$i.'.eml';
if (file_exists($fn)) {
return mkname(++$i);
} else {
return $fn;
}
}

Audio File .wav Corrupted after download with php

Regards,
Using CodeIgniter-PHP and I have a Link (Download Audio) which makes the call to my controller that has the function of downloading audio in. Wav.
As parameter I send the corresponding filename.
Here is my code on my controller:
public function downloadFile($filename) {
$filepath = base_url().'audiofiles/'.$filename;
$filesize = filesize($filepath);
header('Content-Description: File Transfer');
header('Pragma: public');
header('Expires: 0');
header("Content-Type: application/force-download");
header('Content-Type: application/octet-stream');
header("Content-Type: application/download");
header("Content-Length: ".$filesize);
header("Content-disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($filepath);
}
When I click the link I download the wav audio properly, but when I try to play the audio I get that "The file corrupt" - "It needs a special codec"
What is it I'm doing wrong in my code?
Thanks in advance.

Resources