I have a Form where among other info, a user would upload an image. I want to store the path to the image on the database and save the image to the public/img/ folder on the Server.

该表单是使用:{{ Form::open(['route'=>'pizzas.store', 'files'=>true]) }}打开的,这样它就可以发布文件了.判断HTML时,我有以下情况:

<form method="POST" action="http://mypizza/pizzas" 
  accept-charset="UTF-8" enctype="multipart/form-data">

I can POST to my Controller and receive all the data from the Form as expected. The method handling the file is as follows:

public function store() {
    //TODO -  Validation

    $destinationPath = '';
    $filename        = '';

    if (Input::hasFile('image')) {
        $file            = Input::file('image');
        $destinationPath = '/img/';
        $filename        = str_random(6) . '_' . $file->getClientOriginalName();
        $uploadSuccess   = $file->move($destinationPath, $filename);
    }


    $pizza = Pizza::create(['name'       => Input::get('name'),
                           'price'       => Input::get('price'),
                           'ingredients' => Input::get('ingredients'),
                           'active'      => Input::get('active'),
                           'path'        => $destinationPath . $filename]);

    if ($pizza) {
        return Redirect::route('pizzas.show', $pizza->id);
    }

    //TODO - else
}

当我 Select 一个文件并提交表单时,一切似乎都正常,除了/img文件夹中没有保存任何文件.数据库正确注册文件路径和名称.

Running dd($uploadSuccess);right after the if { ...}block, I get the following:

object(Symfony\Component\HttpFoundation\File\File)#220 (2) {
  ["pathName":"SplFileInfo":private]=> string(17) "/img\YZmLw7_2.jpg"
  ["fileName":"SplFileInfo":private]=> string(12) "YZmLw7_2.jpg" }

What am I doing wrong?

推荐答案

你的$destination_path美元是错的.您必须将/public目录的路径包括在变量$Destination中,如下所示:

$destinationPath = public_path().'/img/';

Laravel相关问答推荐

从8.0更新到10.0后,图像不再上传到存储

Nginx上带有SSL的Laravel混响

Laravel Homestead vagrant up 超时

如何在 laravel 5.3 中验证没有 auth:api 中间件的用户?

如何在 Laravel Passport 中获取刷新令牌?

限制 Blade foreach 循环中的结果

将 Laravel MYSQL 更改为 utf8mb4 以在现有数据库中支持表情符号

你如何在自定义 Laravel Nova 工具中使用确认对话?

如何从 Laravel 中的 hasMany() 关系中获取所有结果?

带有时间戳的 Laravel 5.1 eloquent::attach() 方法

如何在 Laravel 中将秒转换为天小时分钟

在 Laravel 4 迁移中创建 MYSQL 过程

Web 服务器如何处理请求?

将图标添加到 Laravelcollective 提交按钮

在共享主机中仅使用 FTP 部署 Laravel 5

改变模式生成器中的列长度?

你如何在 Laravel 中的每个响应上强制一个 JSON 响应?

使用哪个 Auth::check() 或 Auth::user() - Laravel 5.1?

Laravel 数据库模式中的 MediumBlob

count() 参数必须是数组或在 laravel 中实现可数的对象