Good way to show PDF on page from Storage - php

I'm trying to stream PDF files so it's visible on the necessary page, but what I've tried up to now doesn't work at all. I'm trying BarryVdh/domPDF right now, but keep getting "This file extension is forbidden" and I don't know how to fix it.
public function paycheck($id)
{
$payments = Payment::findOrFail($id);
$filename = $payments->file_name;
$foldername = storage_path('app/public/loonstrookjes');
$pathToFile= '/'. $foldername . '/' . $payments->filename;
$pdf = PDF::loadFile($pathToFile)->stream($filename . '.pdf');
dd($pdf);
$file_url = Storage::url('app/public/' . $payments->file_name);
return view('paychecks.details', compact('payments', 'file_url', 'pdf'));
}
This is the function I'm trying to use right now, which is giving the "forbidden" error. Eventhough last time I checked the dd does show the right file path and name.
The ideal image would be a password having to be filled in before the PDF gets shown, but until then it would be great to get the PDF to show in general.
Please I hope anybody will be able to help.

Try this and see if it will display the document first
$payment= Payment::find($id);
return response()->file(public_path().'/documentfolder/'.$payment->file_name);

Related

Image uploader working perfectly for an insert form but not for an update form

I know there are already many similar questions like this and I apologize in advance for adding to the file, but I am a little short on time to do research and I need quick help. I am trying to finish an overdue assignment and my image upload function is working perfectly when I add a product, but not when I update it. I have no idea why. My code to update the image is here:
require_once 'file-util.php'
// Check if the file exists before setting it
if (isset($_FILES['imageFile1'])) {
// Retrieve the name of the file based on what it was called on the client computer
$filename = $codeInput . '.png';
// Make sure the filename exists
if (!empty($filename)) {
// Store the temporary location of where the file was stored on the server
$sourceLocation = $_FILES['imageFile1']['tmp_name'];
// Build the path to the images folder and use the same filename as before
$targetPath = $image_dir_path . DIRECTORY_SEPARATOR . $filename;
// Move file from temp directory to images folder
move_uploaded_file($sourceLocation, $targetPath);
}
}
This is the exact same code that I have in my insert_product file.
And my file_util is here:
$image_dir = 'images';
$image_dir_path = getcwd() . DIRECTORY_SEPARATOR . $image_dir;
Everything else works perfectly, but it is just this little thing that isn't seeming to do anything, so it seems to me like there's a little detail I'm missing for this to work in update_product. Is there something else I need to do to get this to work, or is it something else I'm unaware of?
Edit: Turns out that I just forgot to set the encryption type in my add_product_form. If anyone else has this silly issue, double check your forms for this near the top of the body:
<form action="insert_product.php" method="post"
id="add_product_form"
enctype="multipart/form-data">
You need to check if your updating form tag has the proper enctype attribute value...
and please be aware to use more validation on the uploaded file, your checking for file name exists or not will always be true as you are setting a value for it in the previous line.
Apparently, my code was right but I just forgot to go "enctype="multipart/form-data" in update_product_form.php.

is it possible to send a zipped folder from server (php) to front end (angular) ? or is there another way to do it

I am using drupal as a back end.
in drupal I get some .pdf files. and then zip them using drupal zip archive. then saving this archive in my server tmp folder, i get the tmp folder using php sys_get_temp_dir()
now...
what should i return to the front end (Angular) so that the user can download this folder..
this is an example code i used for zipping:
$nodesIds = [1024, 1023, 1022]; // those are just some example ids, i get the real ids from the front end post request.
$zipName = $this->generateUniqueName();
$zip = new \ZipArchive;if(!$zip->open(sys_get_temp_dir() . '/' . $zipName, constant("ZipArchive::CREATE"))) {
return new JsonResponse('could not open zip');
}
foreach ($nodesIds as $id) {
$node = \Drupal\node\Entity\Node::load($id);
$uri = $node->filed_file->entity->getFileUri();
$name = $node->field_file->entity->id() . '_'. $node->field_file->entity->getFilename();
$url = $file_system->realpath($uri);
$zip->addFile($url, $name);
}
$zip->close();
i tried returning a link to the zipped folder in the server:
return new JsonResponse(sys_get_temp_dir() . '/' . $zipName);
but i dont know what to do with that from angular.
Also tried to return a stream using symfony:
$stream = new Stream(sys_get_temp_dir() . '/' . $zipName);
$response = new BinaryFileResponse($stream);
a stream and not a file because the user chooses which files to zip.. so they can choose as much as they want.. it might get to even a 100 .pdf files ..
every thing works fine and i get the zipped folder in tmp.. but now what ?
I want to download this zipped folder to the user browser..
but I do not know how!. should I return the zipped folder, then in angular use it as a blob or somehow deal with it and serve it to the user,,
or maybe the right way is to send back the link to its location in the tmp folder,, and in angular i only access that location and somehow get the folder (i dont know if this is even possible due to permissions and security), or is there another better way that I do not know about.
thank you very much.

Migrating local images to database in Laravel

I'm trying to seed my local images(.jpg) to database, but when i tried to display the images on my website its not showing every other fields works but the image. I guess i may have set the path wrongly. Please can anyone help to fix this issue?
MigrationTable
Product.Php
ProductTableSeeder
div-form
Output
check that exemple :
$path = storage_path('app/public/photo');
$filename = sprintf('%s_%s_%s_Tracking', time(), $lastID, $newOffers->Name_offre);
$file->move($path, $filename.'.'.$extension);
$imageTracking= new ImageTracking();
$imageTracking->offers_id = $lastID;
$imageTracking->imageTracking = $filename.'.'.$extension;
$imageTracking->save();

PHP can't access newly created zip file

Im using Laravel 5.2 and Zipper to create ZIP archive in a fly and download it by user. I think this problem is generall not strictly related to Laravel or Zipper. Steps:
User click link download_all.
First php script create archive.
Next the same script push created file to force user download.
Everything sounds normal but I have strange behaviour that after zip archive is created (point 2) php/server cant see this newly created file.
Both filesize and file_exists on $filePath return false but file exist. I cant read file why?
When I redirect user to $filePath (instead of reading it and pushing to download) everything is okay and user get file. But why I cant access newly creatd file during "script lifetime"? $paths are correct.
Tested on Windows 7 and Unix.
Any idea?
code:
public function downloadAll($id)
{
$sourceFilesDir = 'upload/source/' . $id;
$zipPath = 'upload/source-zip/' . date('Y-m-d-H-i-s') . '.zip';
Zipper::make($zipPath)->add($sourceFilesDir);
$fullPath = public_path($zipPath);
// works
// return response()->redirectTo($zipPath);
$headers = [
'Content-Type: application/zip',
'Content-Transfer-Encoding: Binary',
'Content-Length: ' . filesize($fullPath),
];
// dont works, cant see file
return response()->download($fullPath, basename($zipPath), $headers);
}
Soved by #Holger.
Zipper should be closed to save file.
Proper code:
Zipper::make($zipPath)->add($sourceFilesDir)->close();
->close()
Unfortunately in Zipper docs this is not clearly mentioned.

Laravel 5.1 - Display images stored in Storage folder

I am making a image uploading function for a website using Laravel 5.1. Because of security problems, I want to store those images in /storage/app folder, instead of /public. Image's URL, for example:
/storage/app/images/<image_name>
In order to get image from /storage/app, I set up a specific route for this:
http://app.com/image/<image_name>
To point image requests to /storage:
Route::controllers([
'image' => 'Frontend\ImageController',
]);
In ImageController, here is the code to create response:
public function missingMethod($filename = [])
{
if (is_array($filename)) {
$filename = implode('/', $filename);
}
// storage_path('app') - path to /storage/app folder
$path = storage_path('app') . '/' . $filename;
$file = \File::get($path);
$type = \File::mimeType($path);
return \Response::make($file,200)
->header("Content-Type", $type);
}
My problem is, after using this route to retrieve images, I can't display them in my view. There were no 404, 500 or any error status codes, request URL was it supposed to be, but it just showed a broken image icon. Is there anything wrong with my code?
Thank you!
Sorry guys, a php scripts in my /config folder has leading space, and that caused the http response failed ## Such a nonsense mistake... Thank you all for your help!

Categories