简体   繁体   English

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

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

What I'm trying is to set an image uploader with Laravel 5.我正在尝试使用 Laravel 5 设置图像上传器。

Here is the form.这是表格。

    <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>

Here is the controller function which will be run from the form above.这是将从上面的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');
}

As you might have noticed, this doesn't store the uploaded file under the assets/serviceimages/ directory, which is what I've been trying.您可能已经注意到,这不会将上传的文件存储在assets/serviceimages/目录下,这正是我一直在尝试的。

Basically I've been following this tutorial , but apparently I'm missing something and totally clueless about what it is, even after I had a look at the API documentation .基本上我一直在关注本教程,但显然我遗漏了一些东西并且对它是什么完全一无所知,即使在我查看了API 文档之后也是如此

Inside of the if statement of the controller function, it was confirmed that the $file and $name had what I expected.在控制器函数的if语句中,确认$file$name符合我的预期。

Therefore, I suspect that the problem lies in the move() function.因此,我怀疑问题出在move()函数上。

Any small piece of advice will be appreciated, since this is my first time that I've tried to set an uploader.任何小的建议将不胜感激,因为这是我第一次尝试设置上传器。

Permissions if you are on Linux.如果您使用的是 Linux,则权限。 If that doesn't work try it this way:如果这不起作用,请尝试以下方式:

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

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

Also, this need to be added in ur ()还有这个需要加在ur()

public function update(Request $request, $id)

I found having to use the Storage adapter to use the full file path worked for me.我发现必须使用存储适配器才能使用对我有用的完整文件路径。

$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);
}

Use This Method This Will Work 100%使用此方法 100% 有效

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

     $file = $request->filename;

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

    $post->filename = $file_new_names;

   }

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

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

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