如何解决laravel文件上传问题

我试图将图像上传到我的公用文件夹上,但是我不断收到这样的错误

The "C:\xampp\tmp\phpCE7B.tmp" file does not exist or is not readable.

这是我到目前为止尝试过的代码

public function create(Register $request)
    {
        //Registering Students
        $student=new Student;
        $student->name = $request->input('name');
        $student->username = $request->input('username');
        $student->email = $request->input('email');
        $student->password = bcrypt($request->input('password'));
        $student->gender = $request->input('gender');
        $student->phone = $request->input('phone');
        if ($request->hasFile('image')) {
            $file=$request->File('image');
            $ext=$student->username. "." .$file->clientExtension();
            $path = public_path(). '/images/';
            $file->move($path,$ext);
            $student->image = $ext;
        }
        $student->save();
}

它使用图像将我的信息保存在数据库中,但存储后出现错误。请帮助我解决此问题

“更新”

我不知道发生了什么事,但是现在它自动运行了

a9413164 回答:如何解决laravel文件上传问题

使用public_path()

时,我也遇到了同样的问题

尝试类似这样的操作,而不要使用public_path。实际上,您的代码没有错。但是由于权限级别,大多数情况下会发生这种情况。正如其他人所说,您可以尝试重新安装Xampp或尝试执行此操作。

if ($request->hasFile('image')) {
    $file = $request->File('image');
    $ext = $student->username.".".$file->clientExtension();

    $ext->save('your_public_path/'.$ext);

    //$path = public_path().'/images/';
    //$file->move($path,$ext);
    $student->image = $ext;
}

我的一个项目中的示例代码供您参考

$image = $request->file('uploadUserAvatar');
$fileName = time().'.'.request()->uploadUserAvatar->getClientOriginalExtension() ;
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300,300);
$image_resize->save(('assets/img/user_avatars/'.$fileName));
,

当我尝试将图像上传到公共路径时,我遇到了同样的问题。 我通过在config(文件系统)中定义一个文件系统磁盘来解决了这个问题:

'YourDiskName' => [
    'driver' => 'local','root'   => public_path(),],

然后您可以使用StoreAs方法将图像上传到此路径:

$file = $file->storeAs(YourPathInPublic,YourFileName,[
    'disk' => 'YourDiskName'
]);
,
// Get Form Image
             $image = $request->file('image');
            $slug = str_slug($request->name);
            if (isset($image))
            {
                $currentDate = Carbon::now()->toDateString();
                $imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
                if (!file_exists('storage/uploads/post'))
                {
                    mkdir('storage/uploads/post',0777,true);
                }
                $image->move('storage/uploads/post',$imagename);
            }else{
                $imagename = "default.png";
            }
//getting coding


 public function index(){

    return view('admin.post.index',compact('posts'));
    }
// show code

<div class="body">
  <img class="img-responsive img-thumbnail" src="{{ asset('storage/uploads/post/'.$post->image) }}" >
    </div>
本文链接:https://www.f2er.com/3157523.html

大家都在问