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

问题描述:

I have a AWS EC2 server with phpMyAdmin to manage it.

Everything is working correctly but I would like to be able to create another folder in the /var/www/html directory to add files..

This is my code but it just keeps returning the error to me! any ideas??

// 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...');
    }

}

I would normally create that folder in the terminal by using sudo mkdir, but when I add sudo Nothing works!

Any help is appreciated! Thanks in advance.

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

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

这是我的代码,但它只是不断返回错误 对我来说! 任何想法?? p>

  // STEP 2.2在服务器中创建一个文件夹来存储posts'pictures 
 $ folder =“/ var / www / html / bloggerFiles / Posts /”  。  $ id; 
 
 
if(!file_exists($ folder)){
 if(!mkdir($ folder,0777,true)){// 0777 
 die('创建文件夹失败...'  ); 
} 
 
} 
  code>  pre> 
 
 

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

感谢任何帮助! 提前谢谢。 p> div>

Make sure the folder(s) you are accessing are set to read and write folder permissions, then use this function:

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

This fixed it for me.

You can create new folder doing this: newFolder('PathToFolder/here', 0777);

EDIT: Please have a look at: https://www.youtube.com/watch?v=7mx2XOFBp8M
EDIT: Also have a look at http://php.net/manual/en/function.mkdir.php#1207

EDIT: Storing functions in classes and safely use the function

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.

EDIT: If this file is called from html it will re create the path, so I will it has to be called from /html/

EDIT: How to use the name_here class

/*
 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
*/