laravel file upload using custam library - php

"I'm tring to upload file using custome library in laravel file goes in folder successfully but file isn't upload in database
this is my custom library:-
namespace App\Classes;
use Illuminate\Http\Request;
class Hello
{
static function jai(Request $request)
{
if($request->hasfile('name'))
{
$image=$request->file('name');
$new_image=time().'.'.$image->getClientOriginalName();
$image->move(public_path('image/'),$new_image);
}
}
}
?>
and this is my controller store function :-
Hello::jai($request);
$x=$request->all();
$x['name']=
$j=Hello::jai($request)->new_image;
Cruds::create($x);
The file "[000004].jpg" was not uploaded due to an unknown error.

Hope this will help.
public function upload(Request $request){
if($file = $request->file('file')){
$name = $file->getClientOriginalName();
if($file->move('files',$name)){
$file= new Files();//DB instance modal
$file->url= $name;
$file->save();
return "success";
}
}

You forgot the file extension. See if this help you.
$image = $request->file('your_input_form_file_name');
$image = time() . '_' . $image->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
$image->move(public_path('images/'), $name);

Related

How to storage link in host?

I upload my project in the host. I uploaded an image, and the image save in database. And the image did not show well, but it show in my localhost as well.
web.php
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
Auth::routes();
Route::get('/link', function(){
return Artisan::call('storage:link');
});
Controller
public function update(Request $request)
{
$user = auth()->user();
$path = $user->image ?? null;
if ($request->hasFile('image'))
{
$file = $request->file('image');
$nane = time();
$extension = $file->getClientOriginalExtension();
$fileName = $nane . '.' . $extension;
$path = $file->storeAs('images/users', $fileName, 'public');
}
$user->image = $path;
}
The image save in database.
The image uploaded in storage/app/public/images/users folder.
The image did not uploaded public/images/users.
Do you have an access to server with ssh? If not you can write like this in your route :
Route::get('/foo', function () {
Artisan::call('storage:link');
});
It will generate the symlink to your public_html, sorry for my bad english

Laravel fail to return image

I've a image and some text file in storage folder other than the public folder. the folder architecture is
storage
|->uploads
and set a route like
Route::get('storage/{folder}/{filename}','FileController#getFile');
inside the Controller the getFile method have following code
$path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);
if (extension_loaded('fileinfo')) {
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
else{
abort(412,'please enable the fileinfo extension or contact site administration');
}
when i try to retrieve the image from url it return the blank image like this
blank image
but when i try to retrive the text file it return the file
Also tried it with Image::make($path) and response()->file($path) when the mime type is image/* still the output is same..
so How do i show my image file from url without using the public folder
This is how i do it... I also check for user permissions but i adapted it to your needs...
public function getFile()
{
$path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);
return response()->file($path);
}

Call to undefined method Intervention\Image\ImageManager::upload()

I am trying to upload an image into my database. When i do upload, i get this error below
Call to undefined method Intervention\Image\ImageManager::upload()
Searching on the internet for solutions, i found this method
adding this line 'Intervention\Image\ImageServiceProvider' in my $providers in config/app.php
adding this line 'Image' => 'Intervention\Image\Facades\Image' in my $aliases in config/app.php
In my controller as well, i have use Image. But then i am still getting this error above. What could i be missing please?
Controller
public function uploadImagePost(UploadUserImageRequest $request)
{
$user = Auth::user();
$image = $request->file('profile_image');
if (false === empty($user->image_path)) {
$user->image_path->destroy();
}
$relativePath = 'uploads/users/' . $user->id;
$path = $relativePath;
$dbPath = $relativePath . DIRECTORY_SEPARATOR . $image->getClientOriginalName();
$this->directory(public_path($relativePath));
Img::upload($image, $path);
$user->update(['image_path' => $dbPath]);
return redirect()->route('my-account.home')
->with('notificationText', 'User Profile Image Uploaded successfully!!');
}
Library you have used doesn't have upload() method. Use save() method for saving the file.
// read image from temporary file
$img = Image::make($_FILES['image']['tmp_name']);
// save image
$img->save('foo/bar.jpg');
Refer this link for more details

How to upload a file (Laravel)

I want to upload a file in Laravel:
I put this code in route
Route::post('upload', 'myconttest#test')->name('upload');
And put this code in controller
function test(Request $request){
$file=$request->file('myfile');
$filename=$file->getClinetOriginalName();
// $projectname;
$path='files/';
$file->move($path,$filename);
}
I create a folder in public called files. The code runs without error but the file is not saved.
Its a working code please see this
public function store(Request $request)
{
$checkval = implode(',', $request->category);
$input = $request->all();
$input['category'] = $checkval;
if ($request->hasFile('userpic')) {
$userpic = $input['pic'];
$file_path = public_path("avatars/$userpic");
if(File::exists($file_path)) {
File::delete($file_path);
}
$fileName = time().$request->userpic->getClientOriginalName();
$request->userpic->move(public_path('avatars'), $fileName);
$input['userpic'] = $fileName;
Product::create($input);
return redirect()->route('productCRUD.index')->with('success','Product created successfully');
}
}
Make sure your form is enabled to upload the file. So check that the following attribute is set or not.
<form action"" 'files' => true>
Use the following namespace
use Illuminate\Support\Facades\Input;
Then try this:
$file = Input::file('myfile');
$filename = $file->getClinetOriginalName();
move_uploaded_file($file->getPathName(), 'your_target_path/'.$filename);
I think you can try this:
First you give folder permission to files folder in public folder
function test(Request $request){
$file=$request->file('myfile');
$filename=$file->getClinetOriginalName();
$file->move(public_path().'/files/', $filename);
}
Hope this work for you !!!
In controller test function you need to given public path of files folder like
$path = public_path('files/');
for moving file on given public folder path.

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

how to delete images file from public/images folder in laravel 5 ??
i found some example from this site, but i know they are just using the file name in their record table, but i'm using something like URL e.g localhost/project/uploads/filename.jpg on my record table. so if i tried like this :
$image_path = $data->image; // the value is : localhost/project/image/filename.format
if(File::exists($image_path)) {
File::delete($image_path);
}
the file is not deleted
help pls, thanks
If you want to 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.
Commonly, Laravel 5 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);
}
If I can delete image from server by reference URL then Google is the first target :)
You can use the normal PHP delete file keyword ( #unlink )
$image_path = "the name of your image path here/".$request->Image;
if (file_exists($image_path)) {
#unlink($image_path);
}
This is what I do to delete an image:
public function SliderDelete(String $slider_id)
{
$slider = Slider::findOrFail($slider_id);
$image_path = public_path("\storage\images\sliders\\") .$slider->photo;
if(File::exists($image_path)) {
File::delete($image_path);
}
else{
$slider->delete();
//abort(404);
}
$slider->delete();
return response()->json(['success'=>'Slider deleted successfully!']);
}
in laravel 8 you do it like
// import Storage class
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->delete('path-of-file');
you can use disk of your choice like
Storage::disk('s3')->delete('path-of-file');
$filename = public_path($fileloc);
if(File::exists($filename)) {
File::delete($filename);
}
call this function and pass two parameter
$filepath = path where your file exist
$filename = name of your file
public static function UnlinkImage($filepath,$fileName)
{
$old_image = $filepath.$fileName;
if (file_exists($old_image)) {
#unlink($old_image);
}
}
public function destroy($id)
{
$imagePath = YourModelName::select('image')->where('id', $id)->first();
$filePath = $imagePath->image;
if (file_exists($filePath)) {
unlink($filePath);
YourModelName::where('id', $id)->delete();
}else{
YourModelName::where('id', $id)->delete();
}
}
To be able to delete the image it should be in the following form :
"/uploads/img1.jpg" where uploads directory is in the public directory then use the following code:
$image_path = puplic_path("/uploads/img1.jpg")
if (file_exists($image_path)) {
File::delete($image_path);
}
Here is the correct and easy way .
if (file_exists(public_path() . '/storage/gallery/images/'. $item->image)) {
unlink(public_path() . '/storage/gallery/images/'. $item->image);
}

Categories