Yii2 - BInary file downloaded can not be open - php

I just create a simple download file function by :
public function actionDownload(){
$id = Yii::$app->request->getQueryParam('id');
// $id = Yii::$app->request->post('file_id');
$path =( new DocumentCRUD())->getDocumentPath($id);
$response = Yii::$app->response->sendFile($this->ROOT_FOLDER.'/'.$path);
$response->send();
}
and in view ,file will be downloaded when click link
window.location.href="document/download?id="+file_id
The problem is that ,after file downloaded ,I can not open binary file such : image ,exe ... just text file is OK ,and im sure that these file on server is no problem
What I have to do ?

I found my problem,just clear all dummy output before send file by ob_end()

Related

Sending image or video file as response in CakePHP 4 is not working

I have tried to send image and video file as response with the code as per CakePHP 4 documentation: https://book.cakephp.org/4/en/controllers/request-response.html#sending-files
Below is my function to access file from url.
public function noteFileAccess() {
$filename = $this->request->getParam('filename');
$id = $this->request->getParam('id');
$path = Configure::read('FilePath.NoteFiles');
$file = new File($path . $id . DS . $filename);
if(!$file->exists()) {
throw new ForbiddenException;
}
$this->response = $this->response->withFile($file->path);
//Also tried to set mime type like below commented line
//$this->response = $this->response->withType($file->mime())->withFile($file->path);
return $this->response;
}
By using above code, it is sending pdf, zip, csv files properly. But images & videos files are not working. Its showing white square in chrome browser. And if downloaded it is showing format not supported in windows system.
Only different I see in downloaded image file vs original image file is image dimensions and some other things like in screenshot below
Edit: File Content Difference added below
I have checked with file path & mime type of the image file is good.
Do you think I am missing anything here?

loading document with error when open with pdf js

I have a strange question regarding PDF.JS and PHP
Using PDF.JS, to open pdf you should use on browser:
http:// ...url... /viewer.html?file=[filename]
I have changed viewer.html extension to viewer.php and developed an additional file document.php to get data from database and load a local PDF file, and I use in this way to get PDF
... viewer.php?file=document.php&control=5E71581C52B96
All work great , AS ESPECTED, but i'm experiencing trouble when get variables from database.
When I Use fileurl like this, PDF load correctly:
$fileurl = 'D:\Drive\_DEV\storage\220\2020-03-17\00000000002\1584486427900.pdf';
When I use fileurl like this (variables from database) PDF not open and have error
Corrupted or inválid PDF Invalid PDF structure
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
If I echo, $fileurl in both cases i have exactly same result.
Regarding SQL QUERY below, on WHERE Clause if I change '$doc_control' (from request) with '5E71581C52B96' (instead '$doc_control') PDF load correctly into pdf.js
If I echo $doc_control, number is exactly same, only difference is how put value on WHERE (number or variable)
If i open document.php work with no problems.
What do I Wrong? Any help is very appreciated.
DOCUMENT.PHP
// REQUEST
$doc_control = $_REQUEST['control'];
// Read database
SELECT *
FROM docs
WHERE doc_control = '$doc_control'
// FILE PATH
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
// READ PDF
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename=".$fileurl);
//#readfile($fileurl);
$file=fopen($fileurl, "r") or die('Unable to open file');
echo fread($file,filesize($fileurl));
fclose($file);

Codeigniter force_download() does not work for PDF extensions

I'm facing two problems.
force_download() does not work for pdf extensions.
when I try to download a txt extension file the functions work fine
below code.
public function index()
{
$data = "some text";
$name = "sample.txt";
force_download($name,$data);
}
but when I try to do it with a pdf extension, the file gets downloaded properly but when I click the downloaded pdf file it shows Error: Failed to load PDF document.
public function index()
{
$data = "some text for pdf";
$name = "sample.pdf";
force_download($name,$data);
}
secondly is there a way to ask or show a dialogue box before downloading a file instead of force download.
You cannot create a (not currupt) pdf file with force_download(). You'll need to create the pdf file using a php library like fpdf or similar. The force_download() function only generates a header which forces a download to happen.
If the pdf file exists on the server you can force_download('/path/to/my_pdf.pdf', NULL);
concerning the second part of your question, this is too broad, there are thousands of ways to create a dialog box
pass name of the file you want to download
Controller
function download($file_name)
{
$this->load->helper('download');
force_download('./assets/'.$file_name, NULL);
}
View
<a href="<?php echo SURL.'controller_name/download/'.$file_name;?>">Download
</a>
For second question try onclick()

Trying to send a tiff file in php to client but the file is damaged after download

I'm trying to send a tiff file from a controller in Kohana framework version 3.2 (I know it's a bit old) using the response->send_file() method, file is downloaded in browser and the size is ok. but when I try to view it I get an error showing the file is damaged. I download the same file using ssh and I can view it without any problems. when I compare the files in notepad++ encoding for the working file is ANSI but for the damaged one is utf-8-bom. this is the code for the method in my controller:
public function action_file() {
$this->auto_render = false;
$path = '/tmp/test.tiff'
$this->response->send_file($path);
}
I read the Kohana send_file source code and I see it is using:
echo fread(...)
to send the file to client.
How can I change the encoding on output buffer so the file be in ANSI (Windows-1252) format?
I tried
mb_http_output('Windows-1252');
mb_internal_encoding('Windows-1252');
with no suuccess
I added ob_clean() before sending file and problem is solved. It seems somewhere in my codes or the framework BOM was added to output buffer and the file was corrupted
public function action_file() {
$this->auto_render = false;
$path = '/tmp/test.tiff';
ob_clean();
$this->response->send_file($path);
}
thanks zerkms for mentioning comparing files in a hex editor

How to upload csv file by class.upload.php?

I am using upload php class from this site: http://www.verot.net/
Image upload is working fine. But when I try to upload csv file it throughs an error:
getimagesize(): Read error! [APP\Vendor\class.upload.php, line 2423]
Here is my code:
$upload = new Upload($file['file']);
$upload->no_script = false;
$upload->allowed = array('application/msword');
$upload->file_new_name_body = 'data';
$upload->process($this->target_path);
if (!$upload->processed) {
$msg = $this->generateError($upload->error);
$this->Session->setFlash($msg);
return $this->redirect($this->referer());
}
Here $file has the all info of attached file.
If I try to upload an image it works fine but when I try to upload a csv file it shows error. I set the mime-type. But no luck. Anyone have this experience. Or is there any plugin like verot.net to upload file. Any idea will be appreciated
This tutorial guides you trought file uploading process:
W3Schools: File Upload
File uploading is not very hard, so I suggest you to try learning it.
Hope this helps!
Your class file is meant to upload or resize image files only.
It will not work for other extensions like .csv, .txt or any other text file or non image file.
I was searching answer to your same question and got that this class should work fine with any other file upload . You just need to set the following for all files other than image
$upload->mime_getimagesize = false;
This will not throw the error you mentioned for non image files

Categories