文件上传laravel 5 [英] file upload laravel 5

查看:97
本文介绍了文件上传laravel 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Laravel 5上传图片

Hi I'm trying to upload an image with Laravel 5

功能是:

$file_name = '';
//validation of image before uploading and saving
if( Input::hasFile('img')  && Input::file('img')->isValid() ){
    $file = Input::file('img'); //creating an object
    $file_name = str_random(30) . '.' . $file->getClientOriginalExtension(); //randon str name to img file with the ext of the original file
    $file->move( public_path() . '\assets\img', $file_name);
}

form:multipart/form-data

form: multipart/form-data

输入:输入type ="file" name ="img"

input: input type="file" name="img"

问题在于$file_name

推荐答案

您的表单开始标签应具有enctype="multipart/form-data".看起来像这样:

Your form opening tag should have enctype="multipart/form-data". Look like this:

<form method="POST" enctype="multipart/form-data">

要在存储中移动图像,请在控制器中编写如下代码:

And to move your image in storage, write your code in controller like this:

public function store(Request $request)
{
    if($request->hasFile('img') && $request->file('img')->isValid()){
        $file = $request->file('img');
        $file_name = str_random(30) . '.' . $file->getClientOriginalExtension();
        $file->move(base_path() . '/assets/img', $file_name);
    }
}

这篇关于文件上传laravel 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆