php - Laravel 5 图片上传不正确的数据库路径

标签 php laravel laravel-5 upload

我的图片上传输入一直有问题。我正在尝试创建一个文件上传输入到我的 Laravel 5 项目中,但我遇到了保存到数据库图像表中的路径问题。

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

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

代码

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

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 基本上用于文件和其他数据验证

View File 似乎喜欢:

{!! 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() !!}

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

关于php - Laravel 5 图片上传不正确的数据库路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30720063/

相关文章:

javascript - 使用 JS 渲染 EMF+ 图像

php - 无法将 Laravel 和 Ajax 数据插入数据库

mysql - Laravel:具有复杂条件的 whereHas()

php - 如何将 excel 文件附加到电子邮件中?

PHP Composer 包含定义了 "use namespace as"语句的文件,附加到每个文件

php - OpenCart 自定义模块 MySQL 错误 1064

php - 尽管已输入输入,但请求包含 null

php - Laravel 日期和时间需要拆分

javascript - Laravel5 ajax 删除不起作用

laravel-5 - Nexmo API 凭据错误