Laravel Image File path not working on server - laravel

I have my laravel application hosted on a server in the root directory as follow:
./Project
I have my images folder inside public folder of the project which reside in public_html with the path as following:
./public_html/project/images
How can i upload image on this path from my controller and also how to retrieve data from there?
what i have tried so far for uploading is:
https://www.mywebsite.com/project/images
but it didn't worked for me. can i have a little help on how to resolve this issue

For inserting data, you can use:
$request->file('image')->store('your-path');
Example:
$name = $request->file('image')->getClientOriginalName();
$path = $request->file('image')->store('public/store/images');
$save = new Photo;
$save->name = $name;
$save->path = $path;
$save->save();
And for retrieving an image:
$getImage = {{asset('images/your-image-name')}}

Related

How to delete photo from project's public folder before updating data in lumen/laravel?

I am trying to delete photo from my projects public folder before updating another photo. My code looks like-
if(File::exist( $employee->avatar)){
//$employee->avatar looks /uploads/avatars/1646546082.jpg and it exist in folder
File::delete($employee->avatar);
}
This is not working in my case. If I comment these code then another photo updated without deleting perfectly. How can I solve the issue!
try php unlink function
$path = public_path()."/pictures/".$from_database->image_name;
unlink($path);
This is the code which i use in update profile picture you can do this exactly like this
$data=Auth::user();
if($request->hasFile('image')){
$image = $request->file('image');
$filename = 'merchant_'.time().'.'.$image->extension();
$location = 'asset/profile/' . $filename;
Image::make($image)->save($location);
$path = './asset/profile/';
File::delete($path.$data->image);
$in['image'] = $filename;
$data->fill($in)->save();
}

how to save dompdf file to storage and name the file dynamicly in laravel

does anyone know how to save generated pdf file and name it dynamicly using dompdf in laravel?
i always encounter this error
failed to open stream: No such file or directory
while trying to generated pdf file and save it to project directory.
here its my controller code
$bills = Tagihan::join('pesanan', 'tagihan.id_pesanan', 'pesanan.id_pesanan')
->join('kendaraan', 'pesanan.id_kendaraan', 'kendaraan.id_kendaraan')
->join('kategori_mobil', 'kendaraan.id_kategori_kendaraan', 'kategori_mobil.id_kategori')
->join('users', 'pesanan.id_pengguna', 'users.id')
->where('pesanan.id_pesanan', $save->id_pesanan)
->select('pesanan.*', 'users.*', 'tagihan.*', 'kendaraan.*', 'kategori_mobil.nama_kategori')
->first();
$today = Carbon::now('GMT+7')->toDateString();
$pdf = new PDF();
$pdf = PDF::loadView('admin.tagihan.pdf', compact('bills','today'));
file_put_contents('public/bills/'.$noOrder.'.pdf', $pdf->output() );
return redirect()->route('pesananIndex');
i want to save my pdf file with custom string with $noOrder.'pdf', but when i use static name like this
file_put_contents('public/bills/bubla.pdf', $pdf->output());
i had no error, any solution?
You can getOriginalContent from http response into a variable then push it to your file.
$pdf = PDF::loadView('admin.tagihan.pdf', compact('bills','today'));
$content = $pdf->download()->getOriginalContent();
Storage::put('public/bills/bubla.pdf',$content);
...
Storage::disk('local')->makeDirectory('/pdf/order');
$pdf = PDF::loadView('PDF.Order.OrderPDF-dev', ['invoiceDetail'=>$invoiceDetail]);
$path = 'storage/pdf/order/';
$pdf->save($path . $fileName);
return url($path.$fileName);
In my example first i created folder for PDF. makeDirectory(...) will create folder if exist or not.
$path is location where i want to put my file in Dir. do not use absolute path or public path function here. only define location where you want to create file.
I am creating file in Storage folder. so i added storage/.....

How to get a file from laravel folder storage for a treatment

I saved an image in storage / app / public and I created a symbolic link to public / storage and the link works well but when I try to get the image with the method Storage :: get ($path) I am being sent a FileNotFoundException. I would like to know how to get my image in my code
$fichier=Storage::get('storage/avatarDebutFemme.png');
Illuminate \ Contracts \ Filesystem \ FileNotFoundException
storage/avatarDebutFemme.png
Previous exceptions
File not found at path: storage/avatarDebutFemme.png (0)
To access your storage path you should use the storage helper for getting the path:
$fichier = Storage::get(storage_path('app/public/avatarDebutFemme.png'));
Would get a file called avatarDebutFemme.png located in the folder storage/app/public of your Laravel project.
Follow ,laravel site it can be helpfull for your problem File Storage-Laravel
tested and works
storage_path() use absolute path
you will need To create the symbolic link
php artisan storage:link
view img tag
src="{{route('employee.get.image', $employee->id)}}"
below is function
$path = storage_path('app/public/'.$image_url);//this will load storage/app/public/
if (!File::exists($path)) {
$path = public_path('images/avatar.jpg');
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type",'image/jpeg');
return $response;

I am trying to upload a file and save the file path to my database so as to be able to access it

I am trying to save file paths to a database and move the files to a given directory. The file path gets save to the database but the files don't get uploaded. Please help
I tried to specify the file path and rename the file
Here is my controller:
public function uploadId(Request $request)
{
$pathOne = $request->file('front_id')->move('public/verify/', 'front_id'.time());
$pathTwo = $request->file('back_id')->move('images/verify/', 'back_id'.time());
$pathThree = $request->file('address')->move('images/verify/', 'address'.time());
$identification = new Identification;
$identification->user_id = Auth::user()->id;
$identification->front_id = $pathOne;
$identification->back_id = $pathTwo;
$identification->address = $pathThree;
$identification->save();
return redirect()->back()->with("success", "Your ID's has successfully been uploaded.");
}
I expect that the file gets saved to the database and uploaded to the directory but that's not the case. The file path only gets saved.
You aren't storing the files anywhere in your code. You can't move a file that hasn't been saved yet.
$path = $request->file('avatar')->store('avatars');

Can't write image data to path in laravel

I am having the same error as this guy is :
Another thread
basically the error i have is uploading the image to the specific path, my code is as follows :
public function postCreate() {
$validator = Validator::make(Input::all() , Product::$rules);
if ($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
$product->image = 'public/img/products'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message' , 'Product created');
}
return Redirect::to('admin/products/index')->with('message' , 'something went wrong')
->withErrors($validator)->withInput();
}
I was just trying to follow a tutorial on laravel e-commerce web application.
I guess the problem is that i don't have write permisson in my directory , how do i add write permission in my directory. I.E. the public folder, I googled a few places , but i don't understand what is it that i have to edit ?
I.E the htaccesss file or can i make write changes on the cmd ? also how do i check what weather a directory is write protected .
PS. i am using windows . i am attaching a screenshot of the error .
Thank you.
You might want to change the dateformat since windows doesn't allow colons in filenames:
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
And you also might want to add a trailing slash to your path so it doesn't concatenate the filename to the folder path:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
Generally this error occurs when you do not yet have the directory that will store the image inside the public directory. Sometimes it can be a permission issue.
Does you img directory exists in your public directory?
To fix this, follow the steps:
Use this snippet:
$relPath = 'img/'; //your path inside public directory
if (!file_exists(public_path($relPath))) { //Verify if the directory exists
mkdir(public_path($relPath), 666, true); //create it if do not exists
}
Or manually create the img directory in public
2.Then you can save your image:
Image::make($image)->resize(468, 249)->save(public_path('img/products'.$filename)); //save you image
$product->image = 'img/products'.$filename; //note
$product->save();
**NOTE: We do not need to specify the public directory in the path because we are using a relative path. The img directory will be created inside public directory.
Along with this, you need to make sure the folder path exists and which has right permissions set.
$relPath = 'img/product/';
if (!file_exists(public_path($relPath))) {
mkdir(public_path($relPath), 777, true);
}
Where $relPath is the path relative to public directory.
This requirement is however windows specific. In linux, folder directory will be created if it does not exist.
I also recommend all of you to check if $path exists. Like Jose Seie use native PHP check, I recommend you to thought about build-in helpers.
This can be achieved with File Facade helper:
File::exists($imagePath) or File::makeDirectory($imagePath, 777, true);
Advice you to use Laravel built-in functions, classes & helpers to improve the performance of your application!
well , i made the correction that john suggested and then made the following corrections :
I replaced the below code :
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products'.$filename);
with :
$path = public_path('img/products/'.$filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
problem solved , i don't know why public_path works , but never mind .

Resources