无法从file.php在/ Var / www / html中创建文件夹

时间:2016-10-01 14:28:14

标签: php amazon-web-services amazon-ec2 phpmyadmin

我有一台带有phpMyAdmin的AWS EC2服务器来管理它。

一切正常但我希望能够在/ var / www / html目录中创建另一个文件夹来添加文件..

这是我的代码,但它只是将错误返回给我!任何想法??

// STEP 2.2 Create a folder in server to store posts'pictures
   $folder = "/var/www/html/bloggerFiles/Posts/" . $id;


if(!file_exists($folder)){
    if (!mkdir($folder, 0777, true)) {//0777
        die('Failed to create folders...');
    }

}

我通常会使用sudo mkdir在终端中创建该文件夹,但是当我添加sudo时没有任何作用!

任何帮助表示赞赏! 提前谢谢。

1 个答案:

答案 0 :(得分:2)

确保您要访问的文件夹设置为read and write文件夹权限,然后使用此功能:

function newFolder($path, $perms)
    $path = str_replace(' ', '-', $path);
    $oldumask = umask(0); 
    mkdir($path, $perms); // or even 01777 so you get the sticky bit set  (0777)
    umask($oldumask);
    return true;
}

这为我解决了。

您可以创建新文件夹:newFolder('PathToFolder/here', 0777);

编辑:请查看:https://www.youtube.com/watch?v=7mx2XOFBp8M
编辑:另请查看http://php.net/manual/en/function.mkdir.php#1207

编辑:在课堂上存储功能并安全使用功能

class name_here
{
    public function newFolder($path, $perms, $deny_if_folder_exists){
        $path = 'PATH_TO_POSTS/'.$path; // This is for setting the root to PATH TO POSTS
        $path = str_replace('../', '', $path); // Deny the path to go out of var/www/html/PATH_TO_POSTS/$path
        if( $deny_if_folder_exists === true ){
            if(file_exists($path)){return false;}
            $old_umask = umask(0);
            mkdir($path, $perms);
            umask($old_umask);

        }elseif( $deny_if_folder_exists === false ){
            $old_umask = umask(0);
            mkdir($path, $perms);
            umask($old_umask);
        }else{
            return false; // Unknown
        }
    }
}
/* Call the function by doing this: */
$manage = new name_here;
$manage->newFolder('test', 777, true); // Test will appear in /var/www/html/PATH_TO_POSTS/$path, but if the folder exists it will return false and not create the folder.

编辑:如果从html调用此文件,它将重新创建路径,因此我必须从/ html /

调用它

编辑:如何使用name_here类

/*
 How to call the function?
  $manage = new name_here; Creates a variable to an object (The class)
  $manage->newFolder('FolderName', 0777, true); // Will create a folder to the path,
  but this fill needs to be called from the html the root directory is set to the
  "PATH_TO_POSTS/" basicly means you cannot do this function from "html/somewhere/form.php",
  UNLESS the "PATH_TO_POSTS" is in the same directory as form.php
*/