繁体   English   中英

Laravel 5:move() 函数不保存上传的图像文件

[英]Laravel 5 : the move() function doesn't save the uploaded image file

我正在尝试使用 Laravel 5 设置图像上传器。

这是表格。

    <form action="/edit/{{$id}}" method="POST" enctype="multipart/form-data">
        {!! csrf_field() !!}
        <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
        <input type="hidden" name="_method" value="PUT">
            //There are other tags (including other input) here
            <input type="file" name="pic" accept="image/*">
        <input type="submit" value="Apply Changes">
    </form>

这是将从上面的form运行的控制器功能。

public function update($id)
{
    $this->model->find($id)->fill($this->request->all())->save();
    if ($this->request->hasFile('pic')) {
        $file = $this->request->file('pic');
        $name = $file->getClientOriginalName();
        $file->move('assets/serviceimages/', $name);
    }

    return redirect('/blahblah');
}

您可能已经注意到,这不会将上传的文件存储在assets/serviceimages/目录下,这正是我一直在尝试的。

基本上我一直在关注本教程,但显然我遗漏了一些东西并且对它是什么完全一无所知,即使在我查看了API 文档之后也是如此

在控制器函数的if语句中,确认$file$name符合我的预期。

因此,我怀疑问题出在move()函数上。

任何小的建议将不胜感激,因为这是我第一次尝试设置上传器。

如果您使用的是 Linux,则权限。 如果这不起作用,请尝试以下方式:

$image = $request->file('pic');
$destinationPathImg = '/your path/images/';

if (!$image->move($destinationPathImg, $image->getClientOriginalName())) {
    return 'Error saving the file.';
}

还有这个需要加在ur()

public function update(Request $request, $id)

我发现必须使用存储适配器才能使用对我有用的完整文件路径。

$storageAdapter = Storage::disk('v1')->getDriver()->getAdapter()

$full_path_before = $storageAdapter->applyPathPrefix($oldPath);
$full_path_after = $storageAdapter->applyPathPrefix($newPath);

if (!File::exists($full_path_before)){
   throw new \Exception("Error moving folders");
}
$move_files = $full_path_after !== $full_path_before;

if ($move_files){
   File::move($full_path_before, $full_path_after);
}

使用此方法 100% 有效

if($request->hasFile('filename')){

     $file = $request->filename;

    $file_new_name =  $file->move("upload/posts/", $file->getClientOriginalName());

    $post->filename = $file_new_names;

   }

记住文件名是你的 <img name="filename">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM