0

I really wonder why I keep getting this error message whenever I try to upload a photo through a form in Symfony2:

Error: Call to a member function move() on a non-object

This is the code that exists in my entity file (Photo.php):

<?php

namespace Boutique\ProduitBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;

use Doctrine\ORM\Mapping as ORM;

/**
 * Photo
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Photo
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="image", type="string", length=255)
     */
    private $image;

    /**
     * 
     * @Assert\File(maxSize="5000k")
     */
    public $file;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set image
     *
     * @param string $image
     * @return Photo
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string 
     */
    public function getImage()
    {
        return $this->image;
    }

    public function getWebPath()
    {
        return null === $this->image ? null : $this->getUploadDir().'/'.$this->image;
    }
     protected function getUploadRootDir()
    {
        // le chemin absolu du répertoire dans lequel sauvegarder les photos de profil
        //return __DIR__.'/../../../../web/groupe/'.$this->getUploadDir();
        return __DIR__.'/../../../../web/'.$this->getUploadDir(); 
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        return 'uploads/pictures';
    }
     public function uploadPhotoPicture()
    {
        // Nous utilisons le nom de fichier original, donc il est dans la pratique 
        // nécessaire de le nettoyer pour éviter les problèmes de sécurité

        // move copie le fichier présent chez le client dans le répertoire indiqué.
        $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());

        // On sauvegarde le nom de fichier
        $this->image = $this->file->getClientOriginalName();

        // La propriété file ne servira plus
        $this->file = null;
    }
}

The code in the controller file:

    <?php

namespace Boutique\ProduitBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Boutique\ProduitBundle\Entity\Photo;
use Boutique\ProduitBundle\Form\PhotoType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{    
   public function ajoutphotoAction(Request $request)
    {
        $photo = new Photo();
        $form = $this->createForm(new PhotoType(), $photo);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $photo->uploadPhotoPicture();
                $em->persist($photo);
                $em->flush();
                $request->getSession()->getFlashBag()->add('success', 'Photo bien enregistrée.');
                return new RedirectResponse($this->generateUrl('boutique_photo_ajout'));
            }
        }
        return $this->render('BoutiqueProduitBundle:Ajoutphoto:ajout.html.twig',
                array('form' => $form->createView()));
    }
}

I would like to know what wrong in my code is. Is there anything missing?

James
  • 4,644
  • 5
  • 37
  • 48
Nadim2014
  • 105
  • 2
  • 10
  • @James: This is the line that contains `move()`: `$this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());` It's inside the function `uploadPhotoPicture()` in `Photo.php` By the way, what I really want is to fix the error. – Nadim2014 Mar 25 '18 at 22:21
  • What is `$this->file`? As far as I can see you just define `$file` as a property and nothing else. Hence why it's be a "non-object". – James Mar 25 '18 at 22:31
  • Actually, following the best answer to the question that was asked about how to upload files on Symfony2 ( you can find it on this [link](https://stackoverflow.com/questions/17951294/symfony2-file-upload-step-by-step) ), I found this code: `$this->file->move( $this->getUploadRootDir(), $this->path );` I also find that `$file` was defined as a property too. Could you please have a look at the link and check that out? Thanks in advance. – Nadim2014 Mar 26 '18 at 08:23

0 Answers0