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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色