沃梦达 / 编程问答 / php问题 / 正文

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

Symfony 2 - UploadedFile, move method failed - Unable to create directory...(Symfony 2 - UploadedFile,移动方法失败 - 无法创建目录...)

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

问题描述

我正在学习 Symfony 2,但在 UploadedFile 类上遇到问题.我想在我的项目中处理文件上传,所以感谢官方文档:如何用 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,你的 web 用户应该是 www-data.所以你需要确保这个用户对你的 web/images 目录有写权限.这可能需要 root 或 sudo 访问权限,具体取决于您的用户权限.但运行此命令以授予 www-data 访问权限:

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-data 用户拥有该目录并创建更多目录(假设所有者具有写入权限).如果您不了解权限,请阅读 Debian Permission 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 - UploadedFile,移动方法失败 - 无法创建目录...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Symfony 2 - UploadedFile,移动方法失败 - 无法创建目录...

基础教程推荐