0

hello guys following the doc to make a file uploader but i cant find out why its not workin when i hit submit its gives an error Unable to create the "uploads/cv" directory although these folders exist already in project/src/My/UserBundle/Resources/public/images

my document entity

namespace My\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Validator\Constraints as Assert;



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


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





/**
 * @var string $path
 *
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $path;

/**
 * @Assert\File(maxSize="6000000")
 */
 private $file ;

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

/**
 * Set path
 *
 * @param string $path
 */
public function setPath($path)
{
    $this->path = $path;
}

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


function getFile()
{
    return $this->file;
}   

function setFile($file) 
{
    $this->file =$file;
}


function preUpload()
{
    if(null !== $this->file )
    {
        $this->path = uniqid() . '.' . $this->file->guessExtension();
    }       
}


function upload()
{
    if(null === $this->file)
    { 
        return ;
    }

    $this->file->move( $this->getUploadDir() ,$this->getPath );
    unset($this->file);
}


function removeUpload()
{
    if($file = $this->getAbsolutePath() )
    {
        unlink($file);
    }
}


function getWebPath()
{
    return $this->getUploadDir() . $this->getPath() ;
}


function getUploadDir()
{
    return 'uploads/cv' ; 
}


function getAbsolutePath()
{
    return $this->getRootDir() . $this->getPath() ;
}

function getRootDir()
{
    return __DIR__ . '../../../../src/' .$this->getUploadDir() ;
}



function setName($n)
{
    $this->name =$n ;
}

function getName()
{
    return $this->name ;
}

}

and my controller action

function uploadAction()
{


    $document = new Document();
    $form = $this->createFormBuilder($document)
        ->add('name')
        ->add('file')
        ->getForm();

    $request = $this->getRequest() ;
    if( $request->getMethod() == 'POST' )
    {
        $form->bindRequest($request);
        if($form->isValid() )
        {
            $em = $this->getDoctrine()->getEntityManager() ;

            $document->upload();
            $em->persist($document);
            $em->flush();

            return
            $this->render('MyUserBundle:Document:upload.html.twig');
        }
    }
            else
            {
             return
             $this->render('MyUserBundle:Document:upload.html.twig' ,array('form'=>$form->createView() );
            } 

thanks in advance

1
  • I just have the same problem and I forget a '/' just after DIR.' and now it works. Try this : return DIR . '/../../../../src/' .$this->getUploadDir() ; Commented Apr 1, 2015 at 13:07

1 Answer 1

1

I think you created your uploads/cv folder in the wrong path.

It should be project/web/uploads/cv, not project/src/My/UserBundle/Resources/public/images.

Then give Symfony the write permissions to that folder with chmod.

Sign up to request clarification or add additional context in comments.

1 Comment

It is possible that I am wrong, but I am not sure you can write in that folder, simply because you don't have write permission there. Maybe some other user could help you more than me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.