Laravel 5.0 files upload validation - laravel

I'm trying to upload multiple files via postman. And would like to validate the files before uploading them. I'm using Laravel 5.0 here is my code to upload the files so far:
if ($request->hasFile('attaches')) {
$files = $request->file('attaches');
foreach ($files as $key => $file) {
$extension = $file->getClientOriginalExtension();
$name = time() .$key. '.' . $extension;
$path = 'files' . '/';
$file = $uploadedFile->move($path, $name);
}
}
This code uploads the files just fine but i would like to validate them before the upload as well. How can I do that in Laravel 5.0?

You can use laravel validater to validate your image. You can do this as
$request->validate([
'image' => 'file|required|mimes:png,jpg,jpeg'
]);

Related

failed when upload multiple image with description in laravel

i am trying to upload image from mobile, with name of the image of it.
i am using flutter for the mobile and using dio to upload it.
but i got error when it upload to laravel server.
here is my code in laravel 9 :
foreach ($request->team as $item) {
$foto = $item->file('id_card');
$path = public_path() . '/upload/idcard/' . Str::slug($workingPermit->id) . Auth::id() . '/images';
$fileName = Str::random(6) . $foto->getClientOriginalName();
$foto->move($path, $fileName);
$workingPermit->tenant_team()->create([
'name' => $item['name'],
// 'id_card' => $item['id_card']
'id_card' => '/upload/idcard/' . Str::slug($workingPermit->id) . Auth::id() . '/images/'. $fileName
]);
}
please help, i really need your help

How can i upload image outside project folder Php Laravel?

I want to save uploaded file to '/home/user/images' folder.
Is there any way to do this?
My 1st try:
controller
public function save(Request $request) {
$file = $request->file('image');
$file_name = $file->getClientOriginalName();
$file_path = '/home/user/images';
$file->move($file_path, $file_name);
}
/////////////////////
My 2nd try:
filesystems.php
'disks' => [
..
'custom_folder' => [
'driver' => 'local',
'root' => '/home/user/images',
],
...
controller
public function save(Request $request) {
$file = $request->file('image');,
$file_name = $file->getClientOriginalName();
Storage::disk('custom_folder')->put($file_name, $file);
}
I'm sorry if there is anything I did wrong. I just started learning php and Laravel.
For now, I save the files in the 'public / images' file path. I will use these files in multiple projects in the future. So I thought of such a method but could not reach the result.
if($request->hasFile('image')){
$image = $request->image;
$image_new_name = $image->getClientOriginalName();
$image->move('storage/custom_folder/', $image_new_name);
}
"storage" folder can be found inside "public" folder; public/storage/custom_folder.
If you want it outside your server dir then create a symlink inside your project folder.

i keep getting "function name must be a string" error

I am building a site using laravel, and I got this error when trying to upload an image. I'm a beginner so, can I get some help?
$user = Auth::user();
$user->name = $request('name');
$user->update();
$file = $request->file('image');
$filename = $request->name . '-' . $user->id . '.jpg';
$user->name = $request('name');
$request is variable not func because of $
Because you didn't follow the naming rules in PHP : $request('name') .
I guess you were going to use $request->get('name')
For Uploading image you can use following code, Although I prefer using Intervention Image
//Auth::loginUsingId(1); #You can test your code without a real login
$data = $request->only(['name']);
if (request()->hasFile('image')) {
$file = request()->file('image');
$fileName = md5($file->getClientOriginalName() . time()) . "." . $file->getClientOriginalExtension();
$destinationPath = public_path('/images/'); //// I assume you have a folder named images in public.
$file->move($destinationPath, $fileName);
$data['avatar'] = $destinationPath.$fileName; // I assume you have a field named avatar in users table.
}
return $data; // Try this line for a better understanding then comment it
Auth::user()->update($data);

How to upload video on laravel

i am trying to upload video on my laravel project it didn't uploading but when i am trying to upload images it will works perfectly
{
$request->validate([
'file' => 'required',
]);
$fileName = time();
$request->file->move(public_path('videos'), $fileName);
$fileupload = new FileUpload;
$fileupload->filename=$fileName;
$fileupload->save();
return response()->json(['success'=>'File Uploaded Successfully']);
}
Well I would store the file first so that the code would rather looks like:
public function uploadVideo(Request $request) {
$request->validate([
'file' => 'required',
]);
$filename = time();
/* Storing the file on the disk */
$request->file->storeAs('videos', $filename);
/* Recording the upload on the database */
$fileupload = new FileUpload;
$fileupload->filename = $filename;
$fileupload->save();
return response()->json(['success'=>'File Uploaded Successfully']);
}
Indeed, in your code, your are moving a file that isn't stored... It can't work fine.
I would suggests you to read more the documentation about File Storage as you would have to use a command to make a symlink :
php artisan storage:link

Class image not found when using intervention

I am using Laravel and Intervention to handle a file upload from the user, I have installed Intervention using Composer but when I try to use some of its functions I get this error message Class 'Intervention\Image\Facades\Image' not found I have checked my app.php file and I have added the correct lines of code to aliases and providers but now I am not sure what is the problem
Here is my function
public function postAvatarUpload(Request $request)
{
$this->validate($request, [
'image' => 'required|image|max:3000|mimes:jpeg,jpg,png',
]);
$user = Auth::user();
$usersname = $user->username;
$file = $request->file('image');
// $ext = $file->getClientOriginalExtension();
$ext= Input::file('image')->getClientOriginalExtension();
$filename = $usersname . '.' . $ext;
if (Storage::disk('public')->has($usersname)) {
Storage::delete($usersname);
}
Storage::disk('public')->put($filename, File::get($file));
$path = public_path('app/public/'. $filename);
Auth::user()->update([
'image' => $path,
]);
$resizedImg = Image::make($path)->resize(200,200);
// $ext = $file->getClientOriginalExtension();
return redirect()->route('profile.index',
['username' => Auth::user()->username]);
}
You also need to import it at the top of the file, after the namespace. Since you say that you've set the facade up, all you need to do is:
use Image;
Add facade and provider as described in the documentation. Then run composer dumpauto -o command.
Then add use Image to your class, or use it like this:
$resizedImg = \Image::make($path)->resize(200,200);

Resources