2

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?

4 Answers4

1

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_);
0

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.

Rajen Trivedi
  • 1,225
  • 2
  • 5
  • 10
  • i have used your suggested method but still getting Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function move() on null if i use hasFile method it returns false in $request – Md.Rafiuzzaman Khan Feb 07 '21 at 06:48
  • that means your form is not sending file, make sure You are using enctype= multipart/form-data in your blade's form tag. – Rajen Trivedi Feb 07 '21 at 12:38
  • i found the file was withing the request object and i can access it like $request->name or $request->_File_ it seems strange but i think how it works with the laravel-admin package – Md.Rafiuzzaman Khan Feb 07 '21 at 12:42
  • that's what I mentioned in my answer, its not strange. – Rajen Trivedi Feb 07 '21 at 13:05
0

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

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');
Pejman Kheyri
  • 4,044
  • 9
  • 32
  • 39
  • it still not working ,heres the log data [2021-02-07 12:21:47] local.ERROR: Method App\Admin\Controllers\ClaimController::validate does not exist. {"exception":"[object] (BadMethodCallException(code: 0): Method App\\Admin\\Controllers\\ClaimController::validate does not exist. at D:\\ivan\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Controller.php:68) [stacktrace] #0 D:\\ivan\\app\\Admin\\Controllers\\ClaimController.php(54): Illuminate\\Routing\\Controller->__call('validate', Array) – Md.Rafiuzzaman Khan Feb 07 '21 at 12:24
  • Which version of laravel & PHP you are using? – Pejman Kheyri Feb 07 '21 at 15:38
  • heres the composer config "require": { "php": "^7.1.3", "dcat-admin-extension/ueditor": "dev-master", "dcat/laravel-admin": "^1.7", "dcat/page": "dev-master", "fideloper/proxy": "^4.0", "guzzlehttp/guzzle": "^7.2", "laravel/framework": "5.8.*", "laravel/tinker": "^1.0" }, – Md.Rafiuzzaman Khan Feb 07 '21 at 16:25
  • Can you do not validate the file and test it? I mean to pass step number 1. – Pejman Kheyri Feb 07 '21 at 16:26
  • some how i could not identify the method you specified in first step ,instead it has something similar hasFile method , i am using a package called dcat-admin – Md.Rafiuzzaman Khan Feb 08 '21 at 06:30
  • Use this instead: $validatedData = $request->validate([ 'File' => 'required|mimes:jpeg,gif,bmp,png|max:2048' ]); – Pejman Kheyri Feb 08 '21 at 06:39