CakePHP mkdir在生产服务器上不起作用

时间:2012-06-17 14:48:45

标签: cakephp

我在admin_add文件中为photocategories_controller.php设置了以下代码。这段代码在家里的CentOS开发服务器上运行得很好。但是,当我将此文件上载到生产服务器时,它不起作用。任何帮助将不胜感激〜

function admin_add() {
    .... //Other code here

    //On folder add, get its ID to be used as the name of the folder
    $folderID = $this->Photocategory->getLastInsertId();

    //This directory is already created
    $uploadDir = '/img/uploads/photos/';
    $serverRoot = $_SERVER['DOCUMENT_ROOT'];

    //Folder location to be created. When I print it I get
    // /home/xxxxxxx/public_html/img/uploads/photos/16 === 16 being the last inserted ID
    $directoryPath = $serverRoot.$uploadDir.$folderID;

    //I CANNOT FIGURE OUT WHAT AM I DOING WRONG HERE. IT WORKS IN MY DEVELOPMENT
    //SERVER, BUT NOT IN MY PRODUCTION SERVER       
    if(mkdir($directoryPath, 0777)){
        if(mkdir($directoryPath."/thumb", 0777)){
            $this->Session->setFlash('Success', 'default', array('class' => 'alert_success'));
        }else{
            $this->Session->setFlash('Error creating thumb', 'default', array('class' => 'alert_error'));
        }
    }else{
        $this->Session->setFlash('Error Creating Directory', 'default', array('class' => 'alert_error'));
    }   

2 个答案:

答案 0 :(得分:0)

解决了!我不知道为什么,但我的生产服务器没有像我预期的那样检测到我的服务器根目录。它将$serverRoot = $_SERVER['DOCUMENT_ROOT'];视为public_html文件夹。但是我需要webroot文件夹。所以我使用了CakePHP自己的WWW_ROOT

答案 1 :(得分:0)

为此,使用API​​函数cakephp文件夹很有用,可以更安全地控制目录的创建

此处提供更多信息

http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html