[英]How can I upload image in local folder laravel?
I am new in larval development, I am unable to upload image in local folder and I am getting error getClientOriginalExtension() on null.我是幼虫开发的新手,我无法在本地文件夹中上传图像,并且在 null 上收到错误 getClientOriginalExtension()。 Even I have added code enctype="multipart/form-data" in my form, but still am getting the error.
即使我在表单中添加了代码 enctype="multipart/form-data",但仍然出现错误。 Can anyone give suggestion how to resolve this issue.
任何人都可以提出如何解决这个问题的建议。 I have given below my code.
我在下面给出了我的代码。 MyBlade file
我的刀片文件
****MyBlade file****
<form action="{{ route('register.post') }}" method="POST" enctype="multipart/form-data" accept-charset="utf-8" >
@csrf
<div class="form-group row">
<label for="uname" class="col-md-4 col-form-label text-md-right">User Name</label>
<div class="col-md-6">
<input type="text" id="username" class="form-control" name="username" required />
@if ($errors->has('username'))
<span class="text-danger">{{ $errors->first('username') }}</span>
@endif
</div>
</div>
<div class="form-group row{{ $errors->has('image') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 col-form-label text-md-right">Profile Picture</label>
<div class="col-md-6">
<input id="image" type="file" class="form-control" name="image">
</div>
</div>
My Controller File我的控制器文件
if ($request->input('image')!=null){
$files = $request->input('image');
$extension = $files->getClientOriginalExtension();
$filaname = time().','.$extension;
$files->move('public/image/',$filaname);
// $post->image= $filaname;
}
else{
return $request;
}
I am getting err:Call to a member function getClientOriginalExtension() on bool我收到 err:Call to a member function getClientOriginalExtension() on bool
If you use $request->file('image')
instead of $request->input('image')
you have fixed the problem.如果你使用
$request->file('image')
而不是$request->input('image')
你已经解决了这个问题。 The image will then be moved to the public/image
folder.然后图像将被移动到
public/image
文件夹。
However, if you want to upload the file to the storage/public
folder, you can use the storeAs()
method on the $file to store it in the storage
folder.但是,如果要将文件上传到
storage/public
文件夹,则可以使用 $file 上的storeAs()
方法将其存储在storage
文件夹中。
// Your controller
public function update(Request $request, $postId)
{
if (! $request->hasFile('image')) {
return $request;
}
// Use file() instead of input()
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
// Note you have a typo in your example, you're using a `,` instead of a `.`
$filename = time().'.'.$extension;
// To store (move) the file to the 'public/image' folder, use this:
$file->move('image', $filename);
// To store the file to the 'storage/app/public/image' folder, use this:
$file->storeAs('public/image', $filename);
// Find your post based on some id
$post = Post::find($postId);
// Save the filename to the database on the Post model:
$post->image = $filename;
$post->save();
}
Hope I helped you with this.希望我能帮到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.