1

我想在我的管理数据中使用 Laravel 存储文件系统上传图像。但是,当我尝试上传图片时出现错误。

在 null 上调用成员函数 getClientOriginalName()

控制器

public function store(Request $request)
{
    $admin              = $request->all();
    $fileName           = $request->file('foto')->getClientOriginalName();
    $destinationPath    = 'images/';
    $proses             = $request->file('foto')->move($destinationPath, $fileName);

    if($request->hasFile('foto'))
    {
        $obj = array (
            'foto'  => $fileName,
            'nama_admin' => $admin['nama_admin'],
            'email' => $admin['email'],
            'jabatan' => $admin['jabatan'],
            'password' => $admin['password'],
            'confirm_password' => $admin['confirm_password']
        );
        DB::table('admins')->insert($obj);
    }
    return redirect()->route('admin-index');
}

看法

<div class="form-group">
    <label for="" class="col-md-4">Upload Foto</label>
    <div class="col-md-6">
        <input type="file" name="foto">
    </div>                
 </div>

错误 错误

4

2 回答 2

0

您可以通过以下方式检查您是否正在获取文件var_dump($request->file('foto')->getClientOriginalName());

并确保您的表格已enctype="multipart/form-data"设置

<form enctype="multipart/form-data" method="post" action="{{ url('/store')}}">
<div class="form-group">
    <label for="" class="col-md-4">Upload Foto</label>
    <div class="col-md-6">
        <input type="file" name="foto">
    </div>                
</div>
</form>
于 2019-02-04T20:55:40.937 回答
0

由于客户端的错误

<form enctype="multipart/form-data" method="post" action="{{ url('/store')}}">
<div class="form-group">
    <label for="" class="col-md-4">Upload Foto</label>
    <div class="col-md-6">
        <input type="file" name="foto">
    </div>                
</div>
</form>

您需要enctype="multipart/form-data"在表单中添加

如果您正在使用form builder version

{!! Form::open(['url' => ['store'],'autocomplete' => 'off','files' => 'true','enctype'=>'multipart/form-data' ]) !!}

{!! Form::close() !!}

然后在你的控制器中你可以检查请求是否有文件

我已经创建了简单方便的功能来上传文件

打开你的控制器并粘贴下面的代码

private function uploadFile($fileName = '', $destinationPath = '')
    {
        $fileOriginalName = $fileName->getClientOriginalName();
        $timeStringFile = md5(time() . mt_rand(1, 10)) . $fileOriginalName;
        $fileName->move($destinationPath, $timeStringFile);
        return $timeStringFile;
    }

以及store方法

Eloquent方法

public function store(Request $request)
    {
        $destinationPath    = public_path().'images/';

        $fotoFile='';

        if ($request->hasFile('foto'))
        {
            $fotoFile= $this->uploadFile($request->foto,$destinationPath    );
        }
        Admin::create(array_merge($request->all() , ['foto' => $fotoFile]));
        return redirect()->route('admin-index')->with('success','Admin Created Successfully');
    }

DB门面版

如果您DB使用 的是use Illuminate\Support\Facades\DB; in top of your Controller

public function store(Request $request)
    {
        $admin              =  $request->all();    
        $destinationPath    = public_path().'images/';

        $fotoFile='';

        if ($request->hasFile('foto'))
        {
            $fotoFile = $this->uploadFile($request->foto,$destinationPath    );
        }

        $obj = array (
            'foto'  => $fotoFile,
            'nama_admin' => $admin['nama_admin'],
            'email' => $admin['email'],
            'jabatan' => $admin['jabatan'],
            'password' => $admin['password'],
            'confirm_password' => $admin['confirm_password']
        );

        DB::table('admins')->insert($obj);

        return redirect()->route('admin-index');
    }

希望清楚

于 2019-02-05T15:33:18.973 回答