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

梦想的初衷 提交于 2019-12-23 12:04:58

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/21561094/symfony-2-uploadedfile-move-method-failed-unable-to-create-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!