Reputation: 29
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
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
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
Reputation: 4832
You can use these steps below:
$this->validate($request, [
'File' => ['required', 'mimes:jpeg,gif,bmp,png', 'max:2048']
]);
$image = $request->file('File');
// 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()));
$tmp = $image->storeAs('uploads', $filename, 'public');
Upvotes: 0
Reputation: 50
You can use laravel storage: https://laravel.com/docs/8.x/filesystem#the-public-disk
just need 3 basic steps
Upvotes: 0
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