dongyuan9149 2016-10-01 14:28
浏览 346
已采纳

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

  • 写回答

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)
        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
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀