Laravel 5图片上传错误的数据库路径 [英] Laravel 5 Image Upload Incorrect Database Path

查看:93
本文介绍了Laravel 5图片上传错误的数据库路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图片上传输入出现了问题.我正在尝试将文件上传输入创建到我的Laravel 5项目中,但是我遇到了保存在数据库映像表中的路径问题.

I have been having issues with my image upload input. I am attempting to create a file upload input into my Laravel 5 project but I am running into problems with the path that is saved into the database image table.

该表单正在工作并且正在发布,但是,当数据库保存图像的路径时,它正在输入:/Applications/MAMP/tmp/php/phptvwTYW而不是仅获取文件名.

The form is working and is posting, however, when the database saves the path to the image it is inputting: /Applications/MAMP/tmp/php/phptvwTYW instead of taking just the file name.

此外,文件正在移动到正确的public/img文件夹.

Additionally, the file is being moved to the correct public/img folder.

代码

public function store(PostRequest $request)
{
    $this->createPost($request);

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

    $filename = $request->file('image_url')->getClientOriginalName();

    $request->file('image_url')->move( $destinationpath,$filename );

    flash()->success('Your Post Has Been Created!');

    return redirect('posts');
}

推荐答案

这是我的项目中当前使用的示例Controller Function

Here is the sample Controller Function currently using in my project

public function postCreateInternal(CreateDocumentInternalRequest $request) {
        $data_information = $request->only(['title', 'assigned_to', 'folder_id', 'document_source']);
        if ($request->hasFile('file_name') && $request->file('file_name')->isValid()) {
            $document = $request->file('file_name');
            #creating file Name
            $mytime = Carbon::now();
            $date_now = $mytime->toDateTimeString();
            $date_now = $this->prepareFileNameString($date_now);
            $document_extension = $document->getClientOriginalExtension();

            $document_name = $this->prepareFileNameString(basename($document->getClientOriginalName(), '.' . $document_extension));
            $document_fullName = $document_name . "_" . ($date_now) . "." . $document_extension;
            $data_information['file_type'] = $document->getMimeType();
            $data_information['file_size'] = $document->getSize();
            $data_information['file_name'] = $document_fullName;
            $data_information['file_download_type'] = "Internal";
            $document->move(public_path() . '/uploads/documents/', $document_fullName);
        }
        if ($pot = $this->document->create($data_information)) {
            $this->notification_admin->sendCreateDocument($data_information);
            return redirect()->route('documents')->with('success', trans('document.create.msg_success'));
//          return redirect()->route('update/document', $pot->id)->with('success', trans('document.create.msg_success'));
        }
        return redirect()->route('create/document')->with('error', trans('document.msg_error'));
    }

CreateDocumentInternalRequest主要用于根据Laravel 5进行文件和其他数据验证

CreateDocumentInternalRequest basically using for File and other data validation as per Laravel 5

查看文件"看起来像:

{!! Form::open(["class"=>"form-horizontal","data-parsley-validate"=>"data-parsley-validate",'role'=>'form','files'=>true]) !!}
<div class="form-group  required {{ $errors->first('file_name', ' has-error') }}">
    {!!Form::label('file_name', trans('document.label.file_name'), array('class' => 'col-md-4 control-label left-label'))!!}
    <div class="col-sm-6">
        {!! Form::file('file_name') !!}
        {!! $errors->first('file_name', '<span class="help-block">:message</span>') !!}
    </div>
</div>
{!! Form::close() !!}

在我当前的实现中,首先我要检查上传的文件,使用当前时间戳重命名文件名,然后重新上传我想要的位置. 如果您需要任何帮助,请提供我提供的方法,让我知道以更好的方式进行改进.

In my current implementation, first i'm checking file uploaded, rename filename with current timestamp and re upload my desire location. If you need any help my provided method let me know to improve in better way.

这篇关于Laravel 5图片上传错误的数据库路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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