【问题标题】:Trying to upload and play video in laravel 4.尝试在 laravel 4 中上传和播放视频。
【发布时间】:2015-02-18 17:39:58
【问题描述】:

所以我想在我的视图中上传和播放视频,但是当我尝试上传 video.mp4 时出现此错误,我不知道它是否会在我的视图中播放。当我尝试上传视频时出现此错误。在非对象上调用成员函数 getRealPath()

我该怎么办?我的意思是如何上传视频并在我的views.blade.php 中播放

所以这是我上传图像/视频的控制器

$image = Input::file('image');
        var_dump($image->getRealPath());
        $filename = $image->getClientOriginalName();

        //saving image to public folder
        Image::make($image->getRealPath())->resize('150', '150')->save('public/img/' .$filename);
        $users = Auth::user();

        //saving image to database
        $post = new Post();
        $post->title = Input::get('title');
        $post->content = nl2br(Input::get('content'));
        $post->image = $filename;

        $users->post()->save($post);

        return Redirect::route('profile', array('id' => $users->id));

这是我上传文件的 view.blade.php

@section('content') 

    <div class="create-post">
        {{Form::open(array('url' => 'Newpost', 'files' => 'true', 'method' => 'post'))}}

            {{Form::label('title', 'Title')}}
            {{Form::text('title')}}<br>

            {{Form::label('content', 'Content')}}
            {{Form::textarea('content')}}

            <br>
            {{Form::file('image')}}
            {{Form::submit('Publish')}}

        {{Form::close()}}
    </div>
@stop

【问题讨论】:

  • 如果你只使用 var_dump($image),显示对象信息?
  • 不,它确实有效,而是返回此错误调用非对象上的成员函数 getRealPath()

标签: laravel


【解决方案1】:

如果您上传的文件大于php.ini 中的upload_max_filesizepost_max_size,则 Input::file('image') 返回 null,作为解决方法,请先尝试评估是否为 null像这样:

<?php

$image = Input::file('image');
if($image) {
    var_dump($image->getRealPath());
    $filename = $image->getClientOriginalName();

    //saving image to public folder
    Image::make($image->getRealPath())->resize('150', '150')->save('public/img/' .$filename);
    $users = Auth::user();

    //saving image to database
    $post = new Post();
    $post->title = Input::get('title');
    $post->content = nl2br(Input::get('content'));
    $post->image = $filename;

    $users->post()->save($post);

    return Redirect::route('profile', array('id' => $users->id));
}

如果为 null 则显示错误消息。

这可行,但强烈建议在您的代码中实现file size validation

希望对你有用。

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 2011-09-28
    • 2023-04-01
    • 2011-11-05
    • 2011-08-02
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多