pdf document error on download - codeigniter

Hello I have some pdf files in my website which the users can download but the problem is when we download these pdf file and try to open the pdf I get an error " There was an error in opening the document. The file is damaged and could not be repaired" but on the contarary this works fine in my local system.
Please guide me , Here is my code
function download_pdf($parameters)
{
$final_link = "$parameters";
$path = strtolower($final_link);
$file = "$path";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
else
{
//echo "file do not exist";
redirect('site/file_does_not_exist');
//$data['main_content'] = 'not_found';
//$this->load->view('includes/template',$data);
}
}

Try using this instead:
...
header('Content-Type: application/pdf');
...
From Internet media types in wikipedia:
application/pdf: Portable Document Format
And also make sure that the file name has the extension ".pdf".

Related

PhpWord mkdir(): Permission denied

Hello I am trying to export a word document on Codeigniter project with XAMPP on mac but I cannot solve the permission problem. I know there are many similar questions but I try many things nothing helps. Here is my save method.
When comment the headers part browser show me 4 Errors. Otherwise I can download the file but in text file same errors again printed.
public function saveWord()
{
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$file = 'HelloWorld.docx';
// header("Content-Description: File Transfer");
// header('Content-Disposition: attachment; filename="' . $file . '"');
// header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
// header('Content-Transfer-Encoding: binary');
// header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
// header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
}
1.Message: mkdir(): Permission denied
Filename: Writer/AbstractWriter.php
2.Message: tempnam(): file created in the system's temporary directory
Filename: Writer/AbstractWriter.php
3.Message: ZipArchive::close(): Failure to create temporary file: No such file or directory
Filename: Shared/ZipArchive.php
4.Type: PhpOffice\PhpWord\Exception\Exception
Message: Could not close zip file php://output:
Filename:
/Applications/XAMPP/xamppfiles/htdocs/kvkkolay/vendor/phpoffice/phpword/src/PhpWord/Shared/ZipArchive.php
I also try to change folder owner with chown -R myusername:mygroup path/to/folder
for permission chmod -R 0777 path/to/folder
In addtion to I also check the php user with whoami the result is daemon.
In the source code of phpword I can see that they are trying to create a temp folder on this path /var/folders/45/zwgpkpmx3kj0hypypctn67rh0000gn/T/PHPWordWriter_635a3ef3f02cf5.54543358 also try to change this folder owner and permission but it returns not permitted.
Could any one help?

Laravel Media Library Expected the file to have mimetype image/jpeg, but found a file with mimetype image/png

I have a news aggregator site that grabs an image from a remote URL but the script is breaking with the following error when I try to crop the file after grabbing:
Expected the file at /home/vagrant/code/storage/media-library/temp/pemA8JMvkodnsavxgk5ZLXXHxeU3wo7n/tiny.jpg have mimetype image/jpeg, but found a file with mimetype image/png
I have no control over this image so I was wondering if there is a way to either correct the mimetype or to catch the error and remove the offending article?
Thanks in advance for your help
edit:
For clarity I am grabbing the image with:
$article->addMediaFromUrl($item['article']['image'])->toMediaCollection('article');
and then running:
$article->save();
The model has the following function:
public function registerMediaConversions(Media $media = null): void {
$this->addMediaConversion('listing')
->fit(Manipulations::FIT_CROP, 140, 140)
->performOnCollections('article');
}
To anyone struggling with this in the future, I solved this with:
try {
$article->save();
} catch (InvalidTinyJpg $e) {
echo "Skipped invalid file\n";
}

Copy logs file to another path laravel

I want to get a copy of the logs file using programmability, I just tried to copy the files using this command
$co= Storage::copy('logs/laravel-'.$start.'.log',$filename);
dd($co);
but I have an error which says the file does not exist,
I believe that is because storage looking in the default driver of the config file, how can I tell laravel to look in logs folder when I want to run the copy command, is there any way to do that .
Hi You can get the path of the log file with storage_path() and copy your file anywhere you want with php copy function
I think that is a easy way that you can do it:
<?php
function copyLogFile($logFileName, $destination)
{
$logPathDir = storage_path().'/logs/';
$logFilePath = $logPathDir . $logFileName;
if (file_exists($logFilePath)) {
if( !#copy($logFilePath, $destination) ) {
echo "File can't be copied! \n";
$errors= error_get_last();
echo "COPY ERROR TYPE: ".$errors['type'].PHP_EOL;
echo "COPY ERROR MESSAGE: ".$errors['message'];
}
else {
echo "File has been copied! \n";
}
} else {
echo "The file $logFilePath does not exist";
}
}
// Store the path of destination file
$destination = '/home/azibom/Desktop/backup.log';
// Your log file name
$start = "2020-01-01";
$logFileName = 'laravel-'.$start.'.log';
copyLogFile($logFileName, $destination);
?>

Unable to open file for reading [ file link ] laravel

I am trying to send a mail to multiple recipients with an attachment of file URL , but while hitting the api in postman it's throwing an error unable to open file for reading [ file link ] , but while I am copying file link and opens in browser it's opening perfectly .
I have checked the file permission also and referred to some of the answers on Stackoverflow but nothing helped me, please help me as soon as possible.
$file_name = 'TimeActivityReport' . "_" . time() . '.pdf';
$storage_path = 'public/TimeActivityReport';
// $storage_path = public_path();
$filePath = $storage_path . '/' . $file_name;
// return $filePath;
$exl = Excel::store(new TimeActivityReportExport($all_total_values,$data,$date_totals), $filePath);
if($exl)
{
$fileurl = asset('storage/TimeActivityReport').'/'.$file_name;
// return $fileurl;
}
// return $fileurl;
return Mail::send([], $emails, function($message)use($fileurl,$emails) {
$message->to($emails,'hello')
->subject('test')
->attach($fileurl,[
'as' => 'checkname.pdf',
'mime' => 'application/pdf'
])
->setBody('check');
});
Try this I tested it on my end and it returned the file
Storage::get('./public/TimeActivityReport/'.$file_name);
You can also test if the file exists using:
Storage::disk('local')->exists('public/TimeActivityReport/'.$file_name);
To attach try:
$fileurl = Storage::path('public/TimeActivityReport/'.$file_name);
resource laravel docs

Laravel 5.3 response()->download - File(.doc, .docx) becomes unreadable after downloading

Laravel 5.3
When I download a file (.doc, .docx) from my storage folder it becomes unreadable. If I go to the local folder and open the file however it is valid and readable.
I am using the standard download function, using headers and stuff.. Have a look at my code:
$fileNameGenerate = 'example_filename';
$fileArr = [ 'wierd_filename', 'docx' ];
$cvPath = storage_path('app/example_folder/subfolder/wierd_filename.docx');
$headers = array(
'Content-Type: application/' . $fileArr[1],
);
try {
return response()->download($cvPath, $fileNameGenerate . '.' . $fileArr[1], $headers);
} catch (\Exception $e) {
//Error
return redirect()->back()->with('error', trans('locale.file_does_not_exists'));
}
Does anyone know what is wrong here? Thank you!
Update: I removed headers, it doesn't work with or without them.
Here is how the files render in the 2 different cases:
Try this
public function getDownload()
{
//doc file is stored under storagepath/download/info.docx
$file= pathofstorage. "/download/info.docx";
return response()->download($file);
}
I added:
ob_end_clean();
before:
response -> download
and it worked for me.

Resources