【问题标题】:Laravel file upload image not working on hostingLaravel 文件上传图片无法在主机上运行
【发布时间】:2016-09-01 01:53:33
【问题描述】:

帮助我的代码在 xampp 和 php artisan serve localhost:8000 上工作 但在托管服务器中验证失败。当我尝试上传图片时,返回Errors: The image field is required.

public function store(Request $request)
{  
 $this->validate($request, array(
         'image' => 'mimes:jpeg,png,bmp|required|max:3000'
 ));

Session::flash('success', 'Image been uploaded.');  

return redirect()->route('galleries.index');
}

html

{!! Form::open(['route' => 'galleries.store', 'class' => 'form-inline', 'files' => true]) !!}

{!! Form::file('image', ['required' => '']) !!} 

{!! Form::submit('Upload', ['class' => 'btn btn-success']) !!}

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

【问题讨论】:

    标签: forms laravel


    【解决方案1】:

    Flash 数据

    有时您可能希望将项目存储在会话中仅用于下一个请求。您可以使用 flash 方法执行此操作。使用此方法存储在会话中的数据将仅在后续 HTTP 请求期间可用,然后将被删除。 Flash 数据主要用于短暂的状态消息:

    在这种情况下,您可以使用此源上传文件

    $fileName = $this->getFileName($file);
    $pathUpload = $this->createPath(sprintf('%s', $config['path']));
    $media = $file->move($pathUpload, $fileName);
    
    protected function createPath($paths)
    {
        if (!is_array($paths)) {
            if (!\File::exists($paths)) {
                \File::makeDirectory($paths, $mode = 0777, true, true);
            }
        } else {
            foreach ($paths as $path) {
                if (!\File::exists($path)) {
                    \File::makeDirectory($path, $mode = 0777, true, true);
                }
            }
        }
        return $paths;
    }
    

    函数createPath用于创建文件夹和设置权限。

    使用file_put_content方法上传文件的函数移动

    【讨论】:

      猜你喜欢
      • 2021-07-01
      • 2020-03-21
      • 2019-11-13
      • 2014-12-22
      • 2019-06-22
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2020-10-03
      相关资源
      最近更新 更多