Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function move() on null

Tags:

php

laravel

I cannot figure out how to resolve this issue. It seems when I try to create my article page if I don't select a img it fails with null error. Anyone have any insight?

Error Is Produced via debug mode. Call to a member function move() on null. /home/UNIT3D/app/controllers/Admin/ArticleController.php

    $post->image = null;
    }

    $v = Validator::make($post->toArray(), $post->rules);
    if($v->fails())
    {
        // Suppression de l'image car la validation a échoué
        if(file_exists(Input::file('image')->move(getcwd() . '/files/img/' . $post->image)))    
        {
            unlink(Input::file('image')->move(getcwd() . '/files/img/' . $post->image));

Full File

 <?php   

    namespace Admin;

    use \View;
    use \Request;
    use \Input;
    use \Auth;
    use \Redirect;
    use \Validator;
    use \Session;

    use \Illuminate\Support\Str;
    use \Article;

    class ArticleController extends \BaseController {

        /**
         * Affiche la page d'administration des articles
         *
         * @access public
         * @return post.admin_index_post
         */
        public function index()
        {
            $posts = Article::orderBy('created_at', 'DESC')->paginate(20);
            return View::make('Admin.article.index', array('posts' => $posts));
        }

        /**
         * Ajoute un article
         *
         * @access public
         * @return post.admin_add_post
         */
        public function add()
        {
            if(Request::isMethod('post'))
            {
                $input = Input::all();
                $post = new Article();
                $post->title = $input['title'];
                $post->slug = Str::slug($post->title);
                $post->content = $input['content'];
                //$post->user_id = Auth::user()->id;
                // Verifie qu'une image à était upload
                if(Input::hasFile('image') && Input::file('image')->getError() == 0)
                {
                    // Le fichier est bien une image
                    if(in_array(Input::file('image')->getClientOriginalExtension(), array('jpg', 'jpeg', 'bmp', 'png', 'tiff')))
                    {
                        // Déplace et ajoute le nom à l'objet qui sera sauvegarder
                        $post->image = 'article-' . uniqid() . '.' . Input::file('image')->getClientOriginalExtension();
                        Input::file('image')->move(getcwd() . '/files/img/', $post->image);
                    }
                    else
                    {
                        // Image null car invalide ou mauvais format
                        $post->image = null;
                    }
                }
                else
                {
                    // Erreur sur l'image donc null
                    $post->image = null;
                }

                $v = Validator::make($post->toArray(), $post->rules);
                if($v->fails())
                {
                    // Suppression de l'image car la validation a échoué
                    if(file_exists(Input::file('image')->move(getcwd() . '/files/img/' . $post->image)))    
                    {
                        unlink(Input::file('image')->move(getcwd() . '/files/img/' . $post->image));
                    }
                    Session::put('message', 'An error has occured');
                }
                else
                {
                    Auth::user()->articles()->save($post);
                    return Redirect::route('admin_article_index')->with('message', 'Your article has been published');
                }
            }
            return View::make('Admin.article.add');
        }

        /**
         * Edite l'article voulu
         *
         * @access public
         * @param $slug Slug de l'article à édité
         * @param $id Id de l'article
         * @return post.admin_edit_post
         */
        public function edit($slug, $id)
        {
            $post =  Article::find($id);
            if(Request::isMethod('post'))
            {
                $input = Input::all();
                $post->title = $input['title'];
                $post->slug = Str::slug($post->title);
                $post->content = $input['content'];
                //$post->user_id = Auth::user()->id;

                // Verifie qu'une image à était upload
                if(Input::hasFile('image') && Input::file('image')->getError() == 0)
                {
                    // Le fichier est bien une image
                    if(in_array(Input::file('image')->getClientOriginalExtension(), array('jpg', 'jpeg', 'bmp', 'png', 'tiff')))
                    {
                        // Déplace et ajoute le nom à l'objet qui sera sauvegarder
                        $post->image = 'article-' . uniqid() . '.' . Input::file('image')->getClientOriginalExtension();
                        Input::file('image')->move(getcwd() . '/files/img/', $post->image);
                    }
                    else
                    {
                        // Image null car invalide ou mauvais format
                        $post->image = null;
                    }
                }
                else
                {
                    // Erreur sur l'image donc null
                    $post->image = null;
                }

                $v = Validator::make($post->toArray(), $post->rules);
                if($v->fails())
                {
                    Session::put('message', 'An error has occured');
                }
                else
                {
                    $post->save();
                    return Redirect::route('admin_article_index')->with('message', 'Your article has been modified');
                }
            }
            return View::make('Admin.article.edit', array('post' => $post));
        }

        /**
         * Supprime l'article désiré
         *
         * @access public
         * @param $slug Slug de l'article
         * @param $id Id de l'article
         * @return void
         */
        public function delete($slug, $id)
        {
            $post = Article::find($id);
            $post->delete();
            return Redirect::route('admin_article_index')->with('message', 'This article has been deleted');
        }
    } ?>
like image 845
HD Vinnie Avatar asked Dec 21 '16 17:12

HD Vinnie


1 Answers

You have to check if you got a file before trying to move that file:

if (Input::file('image')) {

    if(file_exists(Input::file('image')->move(getcwd() . '/files/img/' . $post-    >image)))    
        {
            unlink(Input::file('image')->move(getcwd() . '/files/img/' . $post-    >image));
    ....
}
like image 105
Antonio Carlos Ribeiro Avatar answered Nov 07 '22 16:11

Antonio Carlos Ribeiro