Md.Rafiuzzaman Khan
Md.Rafiuzzaman Khan

Reputation: 29

How to save file from $request in laravel?

I have a function in controller

    public function upload(Request $request)
        {
 $file = $request->file('File');
 $destinationPath = 'uploads';
 $file->move($destinationPath,$file->getClientOriginalName());
    
        }

if i send the $request in log it shows something like this

array (
  '_id' => 'tuYDOc644W6DDgAS',
  '_token' => 'FerVRJvJWtnzv91TGuFRpIeT173aD9pH2o9Pqcu9',
  'upload_column' => 'File',
  'id' => 'WU_FILE_0',
  'name' => '134772132_217259409961521_6013657189083751736_o.jpg',
  'type' => 'image/jpeg',
  'lastModifiedDate' => '2/7/2021, 6:44:12 AM',
  'size' => '629882',
  '_file_' => 
  Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => '134772132_217259409961521_6013657189083751736_o.jpg',
     'mimeType' => 'image/jpeg',
     'error' => 0,
     'hashName' => NULL,
  )),
) 

but this is giving a "Call to a member function move() on null"

i am using laravel-admin package , how can i properly save file and get information about the file?

Upvotes: 2

Views: 7391

Answers (5)

mrvivaldio
mrvivaldio

Reputation: 11

Make sure that in your view, in the form tag, there is the enctype="multipart/form-data" attribute, without this, your form will send character strings.

On the other side in your controller,

public function upload(Request $request){
    // Don't forget the validation

    $name = time(). "." . $request->file->extension();
    $destination = "public/your_path"; // You can omit public if you have the necessary configuration for that

    $path = $request->file->storeAs($destination, $name); // $path is the final destination returned by laravel which you can save in the database for later use.
}

Tell me if it's helpful

Upvotes: 0

Md.Rafiuzzaman Khan
Md.Rafiuzzaman Khan

Reputation: 29

I just found , its bit different with laravel-admin package

the request object is

array (

  '_id' => 'tuYDOc644W6DDgAS',

  '_token' => 'FerVRJvJWtnzv91TGuFRpIeT173aD9pH2o9Pqcu9',

  'upload_column' => 'File',

  'id' => 'WU_FILE_0',

  'name' => '134772132_217259409961521_6013657189083751736_o.jpg',

  'type' => 'image/jpeg',

  'lastModifiedDate' => '2/7/2021, 6:44:12 AM',

  'size' => '629882',

  '_file_' => 

  Illuminate\Http\UploadedFile::__set_state(array(

     'test' => false,

     'originalName' => '134772132_217259409961521_6013657189083751736_o.jpg',

     'mimeType' => 'image/jpeg',

     'error' => 0,

     'hashName' => NULL,

  )),
) 

i can get the file name and file like this and store them

Storage::disk('public')->put($request->name, $request->_file_);

Upvotes: 1

Pejman Zeynalkheyri
Pejman Zeynalkheyri

Reputation: 4832

You can use these steps below:

  1. Validate the requested file by your valid mimes:
$this->validate($request, [
    'File' => ['required', 'mimes:jpeg,gif,bmp,png', 'max:2048']
]);
  1. Get the file from the request
$image = $request->file('File');
  1. Rename the given filename
// get the original file name and replace any spaces with _
// For example, Business Cards.png = timestamp()_business_cards.png
$filename = time()."_". preg_replace('/\s+/', '_', strtolower($image->getClientOriginalName()));
  1. Move the image to the location (public)
$tmp = $image->storeAs('uploads', $filename, 'public');

Upvotes: 0

Manish Rajora
Manish Rajora

Reputation: 50

You can use laravel storage: https://laravel.com/docs/8.x/filesystem#the-public-disk

just need 3 basic steps

  1. create symbolic link from command: php artisan storage:link
  2. config your file system in the config directory: public_path('storage') => storage_path('app/public')
  3. save file: Storage::disk('public')->put('file_name', 'file');

Upvotes: 0

Rajen Trivedi
Rajen Trivedi

Reputation: 1302

Try editing this.

From:

$file = $request->file('File');

To:

$file = $request-> File; //Use file input tag's name attribute here.

Hope this will be useful.

Upvotes: 0

Related Questions