File `{$path}` does not exist error in media library laravel - laravel

Can I save image for 2 tables in db with same file request?
in controller
$combo = new Combo;
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$combo->addMediaFromRequest('image')->toMediaCollection('combos');
}
$menuItem = new MenuItem;
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$combo->addMediaFromRequest('image')->toMediaCollection('menu-items');
}
Can someone help me fix this code to save it? Thank you very much for your help

Related

delete old image while updating the field

I am doing CRUD operations for images. when I am doing the update operation image gets updated but the old image doesn't delete. The following code updates only the file name in the database but I need to remove the old image from the destination folder too while updating else the folder size will become too large. Any ideas would be great. Here my code.
<input type="file" name="profileimage" value="{{ isset($editabout) ? $editabout->profileimage : '' }}" class="custom-file-input" id="exampleInputFile">
Here is my controller
public function update(updateAboutusProfile $request, AboutUs $about)
{
$data=$request->only(['profiletext']);
if($request->hasFile('profileimage')){
$profileimage = $request->profileimage->store('aboutus', 'public');
$oldimage = $about->profileimage;
Storage::delete($oldimage);
$data['profileimage'] = $profileimage;
}
$about->update($data);
toastr()->success('Profile updated successfully');
return redirect(route('about.index'));
//
}
What can be the error I need to resolve,
thank you
Try setting the disk you used to store the file
Storage::disk('public')->delete($oldimage);
Delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
Laravel file is locate in public folder.
Example: your files are located in public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
Enjoy ;)
You should save the path of the file to the database and then simply remove it using \Storage::delete() facade.
Store image using hash name
if($request->hasFile('profileimage')){
$store_path = 'aboutus';
$profileimage = $request->profileimage->store($store_path,'public');
$oldimage = $about->profileimage;
$file_address = $store_path.'/'.$request->profileimage->hashName();
\Storage::disk('public')->delete($oldimage);
$data['profileimage'] = $file_address;
}
Store image using original name
if($request->hasFile('profileimage')){
$store_path = 'aboutus';
$profileimage = $request->profileimage->storeAs($store_path, $uploadedFile->getClientOriginalName(),'public');
$oldimage = $about->profileimage;
$file_address = $store_path.'/'.$request->profileimage->getClientOriginalName();
\Storage::disk('public')->delete($oldimage);
$data['profileimage'] = $file_address;
}
Delete the old image while updating
public function update(Request $request, $id)
{
// other updates
if ($request->hasFile('image')) {
//delete old image if exist
if (File::exists(public_path($oldImage))) {
File::delete(public_path(oldImage));
}
//new image process
}
}

Using imagejpeg() in Laravel

I am migrating my existing PHP program to Laravel. I know the upload should be $request->files->store('images'); but this doesn't compress the images beforehand.
I have found Intervention Image plugin for Laravel but I don't want to use it because I want it to be pure PHP only.
My working code from pure PHP was:
$info = getimagesize($tmpFilename);
if ($info['mime'] == 'image/jpeg') {
$image = imagecreatefromjpeg($tmpFilename);
} else if ($info['mime'] == 'image/gif') {
$image = imagecreatefromgif($tmpFilename);
} else if ($info['mime'] == 'image/png') {
$image = imagecreatefrompng($tmpFilename);
}
imagejpeg($image, $path, 60);
I know the images get saved at /storage/app/public/ so I used that path for imagejpeg as well. This is my laravel code:
$tmp_file = $inner->getPathName();
imagejpeg($tmp_file, '/storage/app/public/images/hehe.jpg', 60);
But it doesn't work. It only says
imagejpeg(/storage/app/public/images/hehe.jpg): failed to open stream: No such file or directory
Any help would be appreciated

How to load datafixtures with files in symfony3

I want to load datafixtures with DoctrineFixturesBundle with images but I don't know how to make it work.
I tried this one :
public function load(ObjectManager $manager)
{
$imageGrabTail = new Image();
$imageGrabTail->setTrick($this->getReference('grab-tail'));
$imageGrabTail->setUpdatedAt(new \DateTimeImmutable());
$file = new UploadedFile($imageGrabTail->getUploadDir() . '/63.jpeg', 'Image1', null, null, null);
$imageGrabTail->setFile($file);
$manager->persist($imageGrabTail);
$manager->flush();
}
My method getUploadDir():
public function getUploadDir()
{
return 'uploads/img';
}
But I have an error :
[Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException]
The file "uploads/img/63.jpeg" does not exist
My file 63.jpeg exists on this folder.
Is there someone who can explain to me why it's not working ?
Thanks !
This object UploadedFile is for file that are upload through a request. Here there is no request, so you have to use get_file_contents() function in order to access your data.
Try this function and tell us the result of it.
To solve it I've just followed this thread
How would you add a file upload to a Symfony2 DataFixture?

How to download gpx files in Laravel 5.2

Good afternoon everyone !
I have this simple code in the controller
public function descargarRutaGPX($id){
$buscar = Ruta::find($id);
$file = Storage::get('ddfbdsfv.gpx');
echo $file;
}
If the file exists the browser show "Preppend text" , but if the file doesn't exists the browser shows FileNotFoundException in FilesystemAdapter.php line 61.
Could anyone help to me ?
UPDATED
$buscar = Ruta::find($id);
if (Storage::has('aaaa.gpx')) {
$file = Storage::get('aaaa.gpx');
return response()->download($file);
}else{
echo "NO";
}
This is the error.
The file "Prepended Text" does not exist
You'll probably want to check and make sure the file exists as well as download the file if it does. Try changing your code to the following
public function descargarRutaGPX($id){
$buscar = Ruta::find($id);
If (Storage::has('ddfbdsfv.gpx') {
$file = Storage::get('ddfbdsfv.gpx');
return response()->download($file);
} else {
// file not found. Do something to alert the user
}
}

CodeIgniter - UserAgent

I have the below code to check if the user is coming from a mobile, if they are I want to load a different page but it doesn't seem to be working...
Can anyone shed any light on the below:
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
I keep getting an internal server error, can anyone spot the problem...
Look at your system/core/Loader.php, function view() that will be $this->load->view returns void unless you add third parameter. try with:
$viewData2['content'] = $this->load->view('newquote/policy_xml', $viewData1, TRUE);
and
'''
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData2);
Note: $viewData2 is different with $viewData1
are you calling this in controller?
What is the 500 (internal server) error??
Did you load the library user_agent??
$this->load->library('user_agent');
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
Finally if you want serve the mobile pages, use post post or pre hooks to determine the user agent before view is executed (if you want to play around)
i have one idea, may be help u
you can replace redirect()/$this->load->view() to $this->_showPage("newquote/policy_mobile", 'Customise', $viewData);

Resources