【问题标题】:Laravel upload image via another URL and store it in the databaseLaravel 通过另一个 URL 上传图像并将其存储在数据库中
【发布时间】:2016-06-28 01:21:43
【问题描述】:

我想通过 Laravel 上传(和更新)一张图片。

我希望将数据库中的现有图像名称替换为新名称。

所以我的控制器中有这个:

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');

        }
    }

问题是,当我得到这个时,我得到了这个错误:

从空值创建默认对象

我有一个帖子路线,看起来像这样:

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

所以我的观点是这样的:

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

我的班级只有这些东西:

<?php

class UserImage extends Eloquent{

    protected $table = 'user_image';

    public $timestamps = false;

}

我认为图像消失是因为我正在使用该路线,但我不知道如何修复它...它不会将图像存储在文件夹中,也不会将随机名称存储在数据库..

谢谢大家!

最诚挚的问候,

罗宾

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    尝试:

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

    并将输入从 img 更改为 image:

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

    还要编辑数据库中的数据,请执行以下操作:

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

    无需打开对象

    你的路线也应该是

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

    在你的刀片中:

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

    更新评论

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

    TBH,我认为您的代码不应该如此复杂。您的路线很好,尝试将您的控制器更改为:

    <?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');
    
                }
     }
    
    ?>
    

    【讨论】:

    • 我现在得到的错误是这个:传递给 Illuminate\Validation\Factory::make() 的参数 1 必须是数组类型,给定对象,在 C:\xampp\htdocs\ 中调用RPR\system\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php 在第 211 行并定义
    • 我明天试试。如果可行,我会批准答案!
    • 我尝试了代码,但现在我有另一个错误Call to a member function update() on null
    • 不应该被吐回,除非其他地方的代码有错误。
    • 我使用另一个网址发布图片?所以我的控制器从 URL http://192.168.0.233/RPR/admin/user 转到 http://192.168.0.233/RPR/admin/updateuserpic 我不知道这是否是问题所在。
    【解决方案2】:

    我在其他人的帮助下找到的独奏曲,非常感谢!

    控制器:

    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')->getClientOriginalName();
    
    
            //store
            UserImage::where('uid', '=', Auth::user()->id)->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');
    
                }
     }
    

    视图只是一个普通的输入。

    希望这可以帮助其他人!

    特别感谢 Ashley 扳手

    【讨论】:

      【解决方案3】:

      我使用原始 PHP 来完成这项工作

      $source = $data['Poster']; /* URL of remote image */
      
      $dest = "posters/".$data['Year']; /* Path */
      if (!file_exists($dest)) {
          mkdir($dest, 0777, true); /* Create directory */
      }
      $dest .= "/".$data['imdbID'].".jpg"; /* Complete file name */
      
      /* Copy the file */
      copy($source, $dest);
      

      【讨论】:

        猜你喜欢
        • 2016-09-06
        • 2016-10-12
        • 2021-09-01
        • 2019-07-26
        • 1970-01-01
        • 2011-12-15
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        相关资源
        最近更新 更多