dongshi1880 2015-05-09 17:15
浏览 414
已采纳

Laravel通过另一个URL上传图像并将其存储在数据库中

I want to upload (and update) an image via Laravel.

I want an existing image name in my database to be replaced by a new one.

So I have this in my controller:

public function UpdatePic()
    {
        $rules = array(
            'image' => 'required',
            );

        $random = str_random(40);
        $validator = Validator::make(Input::all(), $rules);

        //process the storage
        if ($validator->fails())
        {
            Session::flash('error_message', 'Fout:' . $validator->errors());
            return Redirect::to('admin/user#tab_2-2')->withErrors($validator);
        }else{

            //define the new random generated string for imagename
            $imagename = str_random(40) . '.' . Input::file('image')->getClientOriginalName();
            //store
            $userimg            = UserImage::find(1);
            $userimg->img       = $imagename;
            $userimg->save();

            //save the image
            $destinationPath = 'public/img/user_img';

            if (Input::hasFile('img'))
            {
                $file = Input::file('img');
                $file->move('public/img/user_img', $imagename);
            }
            //redirect
            Session::flash('success', 'Uw afbeelding is succesvol veranderd!');
            return Redirect::to('admin/user#tab_2-2');

        }
    }

The problem is, When I Got this I'm getting this error:

Creating default object from empty value

I have a post route wich one looks like this:

Route::post('updateuserpic', 'UserController@UpdatePic');

So my view looks like this:

{{ Form::open(array('url' => 'admin/updateuserpic', 'files' => true)) }}
                                                    <div class="form-group">
                                                        <div class="fileinput fileinput-new" data-provides="fileinput">
                                                            <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;">
                                                                <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=Geen+afbeelding" alt=""/>
                                                            </div>
                                                            <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;">
                                                            </div>
                                                            <div>
                                                                <span class="btn default btn-file">
                                                                    <span class="fileinput-new">
                                                                         Selecteer een afbeelding
                                                                    </span>
                                                                    <span class="fileinput-exists">
                                                                         Verander
                                                                    </span>
                                                                    {{ Form::file('image') }}
                                                                </span>
                                                                <a href="#" class="btn red fileinput-exists" data-dismiss="fileinput">
                                                                     Verwijder
                                                                </a>
                                                            </div>
                                                        </div>
                                                        <div class="clearfix margin-top-10">
                                                            <span class="label label-danger">
                                                                 waarschuwing!
                                                            </span>
                                                            <span>
                                                                 Dit is enkel ondersteund in de laatste versies van Firefox, Chrome, Opera, Safari and Internet Explorer 10!
                                                            </span>
                                                        </div>
                                                    </div>
                                                    <div class="margin-top-10">

                                                        {{ Form::submit('Opslaan', array('class' => 'btn green')) }}

                                                        <a href="{{ Config::get('app.url') }}/admin/user#tab_2-2" class="btn default">
                                                             Annuleer
                                                        </a>
                                                    </div>
                                                {{ Form::close() }}

My Class only has this stuff:

<?php

class UserImage extends Eloquent{

    protected $table = 'user_image';

    public $timestamps = false;

}

I think the image disappears because I'm using that route, but I don't know how to fix it... It doesn't store the image in the folder and it doesn't store the random name in the database..

Thanks people!

Kindest regards,

Robin

  • 写回答

3条回答 默认 最新

  • doudu161481 2015-05-09 17:47
    关注

    try:

    validator::make(Input::file('image'), $rules);
    

    and change the Input from img to image:

            if (Input::hasFile('image'))
            {
                $file = Input::file('image');
                $file->move('public/img/user_img', $imagename);
            }
    

    also to edit the data in the databse, do:

     UserImage::find(1)->update(['img' => $imagename]); 
    

    no need to open a object

    also your route should be

    Route::post('admin/updateuserpic', 'UserController@UpdatePic');
    

    in your blade:

    {{ Form::open(array('url' => 'admin/updateuserpic','method' => 'post' ,'files' => true)) }}
    

    Update for comment

    $file = array('image' => Input::file('image');
    validator::make($file , $rules);
    

    TBH, i think your code shouldn't be so complected. your Routes are fine, try changing your controller to:

    <?php
    
     public function UpdatePic(){
    
    //first, I'll just do the file validation
    $validator =  Validator::make(array( 'image' => Input::file('image')), 
                                    array('image' => 'required|image'));
    
    
    if($validator->fails()){
    
            //return error code
            Session::flash('error_message', 'Fout:' . $validator->errors());
            return Redirect::to('admin/user#tab_2-2')->withErrors($validator);
    
        }else{
    
            //update the image name
            $imageName = str_random(40) . '.' . Input::file('image')->getClientOrignalName();
    
            //store
            UserImage::find(1)->update(['img' => $imageName]);
    
            //now move that image to the new location
            $file = Input::file('image');
            $file->move('public/img/user_img/', $imageName);
    
            //now we have done, lets redirect back
            Session::flash('success', 'Uw afbeelding is succesvol veranderd!');
            return Redirect::to('admin/user#tab_2-2');
    
                }
     }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)