Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 - UploadedFile, move method failed - Unable to create directory...

I'm learning Symfony 2, and I have trouble with the class UploadedFile. I want to handle file uploading in my projet so I learned thanks to the official documentation : How to handle File Uploads with Doctrine

It works pretty good. However, it works just in local. I use MAMP for testing my project.

I tried to put my project in Debian 6 server. The Symfony app works, but the upload file doesn't work. When I move the file with the method move() ... I have this error :

Unable to create the "/var/www/project/Symfony/src/project/Bundle/AlbumBundle/Entity/../../../../../web/images/postes_images" directory (500 Internal Server Error)

As I told you, It works very well in local. If the folder postes_images doesn't exist, it supposed to create by itself. It works well in local...

This is my upload function:

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

    $this->name = "poste_".$indexPoste."_pic_".$indexPic;
    $this->path = "poste_".$indexPoste."_pic_".$indexPic.".".$this->file->getClientOriginalExtension();
    $this->file->move($this->getUploadRootDir(), $this->path);

    $this->file = null;
}

Functions to get paths :

public function getUploadRootDir()
{
    return __DIR__.'/../../../../../web/'.$this->getUploadDir();
}

protected function getUploadDir()
{
    return 'images/postes_images';
}

The paths are correct, because it works very well in local. I'm wondering if it's not a problem of configuration of php ... I made some searchs, and I read that in move method, they call "mkdir" method to create the folder if it doesn't exist... If the mkdir call returns false, it returns the error that I put above.

If you have an idea how to resolve this issue please. I couldn't find anything.

Thanks

like image 907
manonthemoon Avatar asked Feb 04 '14 19:02

manonthemoon


1 Answers

If you are using Debian, your web user should be www-data. So you need to ensure that this user has write permission to your web/images directory. This may require root or sudo access, depending on your user permissions. But run this command to give www-data access:

chown -R www-data /var/www/project/Symfony/web/images

This will allow the www-data user to own the directory and create further directories (assuming the owner has write permissions). If you aren't up to speed on permissions, read Debian Permission wiki to understand more.

like image 57
Sehael Avatar answered Oct 19 '22 16:10

Sehael