Migrating local images to database in Laravel - php

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

Related

Laravel does not upload video file and store tmp filename in database

I'm working with Laravel 5.8 and I wanted to upload a video file.
So in the Controller, I added this code which does the uploading:
if ($request->file('prd_video')) {
$video = Request::file('prd_video');
$videoname = $video->getClientOriginalName();
$path = public_path().'/upload/video/products/';
$request->file('prd_video')->move($path, $videoname);
$findpro = Product::find($stored->prd_id);
$findpro->prd_video = $videoname;
$findpro->save();
}
Now when I test this, after minutes of loading, the file does not uploaded and store a name like /tmp/phpsMPjG1 as filename in the database.
Also there is no error appearing and the process of insertion seems to be completed successfully.
So what's going wrong here? How can I solve this issue?
edit: previous answer about version of aws-sdk-php was not right.

Laravel 6 Storage results in a 404 error when trying to fetch files

I have tried to setup an upload script in Laravel and have followed the instructions in the docs.
I created a Symlink using the Laravel script and it looks like the following
storage -> /Users/username/Sites/switch/storage/app/public
The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.
$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);
and this returns
http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.
http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
Shouldn't
echo asset($path);
be returning a url containing storage instead of public?
assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.
Storage::url('file.jpg');
Well, there are a lot of ways to do that, pick anyone which fits you best.
// using storage_path helper
storage_path('public/' . $filename);
// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
// using asset helper
asset('storage/your_folder/image.png');
// using url helper
url('storage/your_folder/image.png');
// using Storage facade
Storage::url($photoLink)
Here is the simplest and exact thing for your issue
if(!empty($request->file('manufacturer_image_name'))){
$path = storage_path('public/image/');
$image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));
//Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()
$manufacturer->manufacturer_image_name = isset($image_path) ? "storage/".$image_path : "";
}
Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder
This was what I ended up with.
if($request->file('manufacturer_image_name')){
$path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));
echo $path;
}
$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.

Good way to show PDF on page from Storage

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

Strange chars....I Want to save this "º" char, but Im saving like "1º"

Im doing a pdf upload system, and it is already working fine.
But, Im always giving to the name of pdf a level, this level have always this char "º".
What is happening is when I do the upload, pdf name is saved no with "º" but with "º".
Do you know how I can solve this?
if(!empty($_FILES['pdf']['tmp_name'])){
$folder = '../pdfs/';
$year = date('Y');
$month = date('m');
$pdf = $_FILES['pdf'];
$ext = substr($pdf['name'],-3);
$f['pdf'] = $f['level'].'.'.$ext;
move_uploaded_file($pdf['tmp_name'], $folder.$year.'/'.$month.'/'.$f['pdf']);
}
In my folder, where I save the files, the pdf name is like this: "1º Level"
In database, I dont have here the code, but it saves also like "1º Level".
I already change the database to utf8_general_ci, but the problem remains, and dont seems a database problem because, also when I save the file in a folder I have the same problem...

Give the name to the output PDF file from MYSQL database

I have been generating the PDFs and download it. Everything works fine. But whenever the file is output, it simply replaces the previous generated file.
Is there any way to output the file with the different name every time the NEW user downloads it by selecting data from the database?? What changes I can make in Output() method?
I have read output() doc.
$mpdf->Output("PDFs/something.pdf");
// change the path to fit your websites document structure
$fullPath = "PDFs/something.pdf";
You can do:
$filename = 'pdf' . time(). '.pdf';
$mpdf->Output("PDFs/$filename");
You can attach some timestamp/date with filename.
$filename = "PDFs/something-" . time() . ".pdf";
$mpdf->Output($filename);
// change the path to fit your websites document structure
$fullPath = $filename;
With the help of the answer given by Ramesh and some help from Immibis, I have found my way like this. Since the 'proposal_id' will always be unique in my case, so there will be no chance of downloading two pdfs of same id at the same time by different users.
$con = mysqli_connect("localhost","root","","my_db");
$name="Select name FROM table_name WHERE proposal_id= '$id'";
$name_result= mysqli_query($con,$name);
$row_name= mysqli_fetch_array($name_result);
$filename = $row_name['name']. time();
$mpdf->Output("PDFs/$filename.pdf");
$fullPath = "PDFs/$filename.pdf";

Categories