Audio File .wav Corrupted after download with php - codeigniter

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.

Related

laravel intervention image not displaying image

Everything seems to be working as I do not get any error but I get garbage code instead of the image itself. The code is very simple in the controller:
// get image & resize
$img = ImgMgr::make('http://pathToImage.jpg')->resize(200,100);
// send HTTP header and output image data
echo $img->response('jpg', 70);
Then I get : HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Length: 2900 Content-Type: image/jpeg Date: Wed, 21 Jul 2021 16:05:21 GMT ����JFIF``��;CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), quality = 70 ��C   #%$""!&+7/&)4)!"0A149;>>>%.DIC;��C  ;("(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;��dd"��
What am I missing?
Here is the solution...
$img = ImgMgr::make('http://pathToImage.jpg')->resize(200,100);
$img->response('jpg', 70);
$type = 'png';
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($img);
<img src="{!! $base64 !!}">

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.

Unable to download pdf file from the server in 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.

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();

Resources