How to upload video on laravel - 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

Related

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.

Laravel 5.0 files upload validation

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

Upload image to two different folder locations using Laravel 5.8

This Code save image in only one folder. I want to upload the image at the same time in two different folders,
example
folder-one
and
folder-two
my controler
protected function validator(array $data)
{
return Validator::make($data, [
'photo_jpeg' => 'required|image|mimes:jpeg,png,jpg|max:2048',
]);
}
protected function create(array $data)
{
$photo_jpeg= time() . '.' . $data['photo_jpeg']->getClientOriginalExtension();
$data['photo_jpeg']->move(base_path() . 'public/folder-one', $photo_jpeg);
return user::create([
'photo_jpeg' => $photo_jpeg,
]);
}
Please make sure these things. if you are going to update file on different location.
The folder must have the writtable permission.
Directory path should be defined absolute and point to correct location.
Now change verify the changes in code as follow.
$fileName = time() . '.' .$request->file('User_jpeg')->getClientOriginalExtension();
$storageLocation = '../../WEBSITE-FILE/TEAM/USER'; //it should be absolute path of storage location.
$request->file('User_jpeg')
->storeAs($storageLocation, $fileName);
$request->file('User_jpeg')
->storeAs($storageLocation . '/User_Profile_Image', $fileName);
Edits:
As per the requested current status, try this.
public function store(Request $request) {
$this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg|max:2048', ]); $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('folder-a'), $input['image']);
$fileSrc = public_path('folder-a') . $input['image'];
$fileDest = public_path('folder-b') . $input['image'];
\File::copy($fileSrc, $fileDest);
Service::create($input);
return back()->with('success',' CREATED SUCCESSFULLY .');
}
In controller:
public function store(Request $request){
if($request->User_jpeg != ''){ //check file has selected
$file = $request->User_jpeg;
$path = base_path('public/folder-one/');
$filename = time() . '_' . $file->getClientOriginalName();
$file->move($path, $filename);
\File::copy($path.$filename,base_path('public/folder-two/'.$filename));
}
user::create([
'photo_jpeg' => $filename,
]);
}
In route file (web.php):
Route::post('save-image', 'YourController#store');

Call to undefined method Intervention\Image\Facades\Image::make()

I upgraded from Laravel 4.2 to Laraveld5.3 with intervention/image : "^2.3",
if (Input::hasFile('logo')) {
$path = public_path()."/assets/admin/layout/img/";
File::makeDirectory($path, $mode = 0777, true, true);
$image = Input::file('logo');
$extension = $image->getClientOriginalExtension();
$filename = "logo.$extension";
$filename_big = "logo-big.$extension";
Image::make($image->getRealPath())->save($path.$filename);
Image::make($image->getRealPath())->save($path.$filename_big);
$data['logo'] = $filename;
}
The result Is, got the error below:
Call to undefined method Intervention\Image\Facades\Image::make()
I experienced the same issue in my Laravel 5.4 project. I stumble on this link
that help resolve the issue. This was the fix that was provided
In config/app change 'aliases' for Image from
'Image' => Intervention\Image\Facades\Image::class,
To
'Image' => Intervention\Image\ImageManagerStatic::class,
Then in your controller header add
use Image;
Make Sure that
In config/app update Providers with
Intervention\Image\ImageServiceProvider::class
and update aliases with
'Image' => Intervention\Image\Facades\Image::class,
In your config/app.php file, add
Intervention\Image\ImageServiceProvider::class,
in providers array and add
'Image' => Intervention\Image\Facades\Image::class,
in aliases array.
Run
php artisan config:cache
command.
In your controller add
use Image;
before class definition.
Now you can use the Image class according to your needs inside the controller's function. suppose,
$imageHeight = Image::make($request->file('file'))->height();
public function optimizeFile(Request $request)
{
$data = $request->file('image');
// dd($data);
if ($request->hasFile('image')) {
foreach($data as $key => $val){
$path=storage_Path('app/public/');
$filename='image-'.uniqid().$key.'.'.'webp';
$val->move($path,$filename);
$p['image']=$filename;
$insert[$key]['image'] = $filename;
//Resize image here
$thumbnailpath[$key]['abc'] = storage_path('app/public/'.$filename);
$img = Image::make($thumbnailpath)->resize(400, 150, function($constraint) {
$constraint->aspectRatio();
});
$img->save($thumbnailpath);
}
return redirect('ROUTE_URL')->with('success', "Image uploaded successfully.");
}
}

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