Published 6 days ago by towhid
this is my #controller
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'cover_image' => 'image|nullable|max:1999',
]);
// Handle File upload
if($request->hasFile('cover_image')){
// get filename with the extention
$fileNameWithExt = $request->file('cover_image')->getClientOrginalName();
// get just filename
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// get just Ext
$extention = $request->file('cover_image')->getClientOrginalExtention();
// Filename to store
$fileNameToStore = $filename.'_'.time().'.'.$extention;
// upload image
$path = $request->file('cover_image')->storeAs('public/cover_images',$fileNameToStore);
}else{
$fileNameStore='noimage.jpg';
}
//create post
$post=new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->user_id = auth()->user()->id;
$post->cover_image = $fileNameToStore;
$post->save();
return redirect('/posts')->with('success','Post created');
}
why this error occurd ?
It should be getClientOriginalExtension
instead of getClientOrginalExtention
.
The t
should be an s
in extension
@bobbybouma after correction some code -
$fileNameWithExt = $request->file('cover_image')->getClientOriginalName();
$extention = $request->file('cover_image')->getClientOriginalExtension();
The cover image failed to upload.
How could we possibly say?
You could just have an echo statement 'The cover image failed to upload.' ?
It's related to validation rules
'uploaded' => 'The :attribute failed to upload.',
in validation.php
@snapey hello , when iam trying to upload image size 1999 then show this error . could you please tell me how to maximize the image size controll ?
@towhid in php.ini
increase upload_max_filesize
o so its depend on - php.ini settings, not any laravel -- end ? @Sergiu17
In your validation you are saying that the file cannot be larger than 1.999 MB
In addition, PHP has upload_max_filesize and also post_max_size
You need to understand what these are to know if your upload will work
@Snapey hi , i already use max: 1999 to max : 5000 but still same error
The cover image failed to upload.
Try uploading a small image, like one that's less than 1mb. Just to test if the upload functionality works.
If it does work for smaller files, then you need to change your php.ini settings and reboot the web server like the others have mentioned.
Yes, it's most likely a php settings issue, not laravel. If the php settings
don't allow uploads greater than 2MB, then, simply, it won't work if the image is greater than that size in Laravel or anything else. PHP itself is preventing it.
You can easily see the php settings by creating this route and going to it.
Route::get('/phpinfo', function() {
phpinfo();
});
It will make a nice HTML page of all of the php settings and enabled extensions. Be sure to delete the route when you're done.
Please sign in or create an account to participate in this conversation.