Symfony 2 - UploadedFile,move方法失败 - 无法创建目录...

时间:2014-02-04 19:01:49

标签: php symfony file-upload debian

我正在学习Symfony 2,我在使用UploadedFile类时遇到了麻烦。 我想在我的项目中处理文件上传,所以感谢官方文档:How to handle File Uploads with Doctrine

效果很好。但是,它只适用于本地。我使用MAMP来测试我的项目。

我试图把我的项目放在Debian 6服务器上。 Symfony应用程序工作,但上传文件不起作用。 当我使用方法move()移动文件时...我有这个错误:

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

正如我告诉过你的,它在当地非常有效。如果文件夹postes_images不存在,它应该自己创建。它在当地很好用......

这是我的上传功能:

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;
}

获取路径的功能:

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

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

路径是正确的,因为它在本地运行良好。我想知道它是不是配置php的问题... 我做了一些搜索,我在移动方法中读到,如果它不存在,则调用“mkdir”方法创建文件夹...如果mkdir调用返回false,则返回我放在上面的错误。

如果您知道如何解决此问题,请。我找不到任何东西。

由于

1 个答案:

答案 0 :(得分:12)

如果您使用的是Debian,则您的网络用户应为www-data。因此,您需要确保此用户具有对web/images目录的写入权限。这可能需要root或sudo访问权限,具体取决于您的用户权限。但是运行此命令可以访问www-data:

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

这将允许www-data用户拥有该目录并创建更多目录(假设所有者具有写入权限)。如果您无法快速了解权限,请阅读Debian Permission wiki以了解更多信息。