Symfony 2 - 已上传文件,移动方法失败 - 无法创建目录... [英] Symfony 2 - UploadedFile, move method failed - Unable to create directory...

查看:135
本文介绍了Symfony 2 - 已上传文件,移动方法失败 - 无法创建目录...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Symfony 2,而且我已经遇到了类FileFile的麻烦。
我想在我的项目中处理文件上传,所以我学习了官方文档:如何用Doctrine处理文件上传

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

它的效果非常好。但是,它只在本地工作。我使用MAMP来测试我的项目。

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

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

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)

正如我所说,它在本地工作得很好。如果文件夹postes_images不存在,它应该自己创建。它在本地工作很好...

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...

这是我的上传功能:

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的配置问题...
我做了一些搜索,我在move方法中读到,如果它不存在,他们调用mkdir方法创建文件夹。 ..如果mkdir调用返回false,它返回上面提到的错误。

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.

谢谢

推荐答案

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

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

这将允许www数据用户拥有目录并创建更多目录(假设所有者具有写入权限)。如果您不能加快权限,请阅读 Debian权限wiki 了解详情。

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.

这篇关于Symfony 2 - 已上传文件,移动方法失败 - 无法创建目录...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆