dongyuan9149 2016-10-01 14:28
浏览 341

无法从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 (!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.

  • 写回答

1条回答 默认 最新

  • dongxinjun3944 2016-10-01 14:47

    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)
        return true;

    This fixed it for me.

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

    EDIT: Please have a look at:
    EDIT: Also have a look at

    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);
            }elseif( $deny_if_folder_exists === false ){
                $old_umask = umask(0);
                mkdir($path, $perms);
                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
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥80 用PE可以更改原win11系统的环境变量吗?
    • ¥20 win10商店接入问题
    • ¥15 java 这种树形框吗
    • ¥40 找同学帮敲Python代码
    • ¥15 MYSQL 订单的商品明细重复计算问题
    • ¥15 微信实时共享位置修改
    • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
    • ¥50 共模反馈回路的小信号增益
    • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
    • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall