Search code examples
phpmkdirscandir

Using VPS server when I create directory using mkdir() it returns true but folder when I check using cpanel is blank,


Using VPS server when I create directory using mkdir() it returns true but folder when I check using cpanel is blank, I dont know why I even used scandir() and I noticed that those folders which I created is showing in an array in scandir(), why is that happening, why those folders are not showing ? This is my code:

/creating directory/

                if (!file_exists('public_html/members/1213121')) {
                    mkdir('public_html/members/1213121', 0777, true);
                    echo "file getting created";
                }
                else{
                    echo "file not getting created.";
                }

                /**this is the code I put to scan the members folder and it retuns array and showing the folder named 1213121 but in actual cpanel that directory is not there **/

                $dir = "public_html/members/";

                // Sort in ascending order - this is default
                $a = scandir($dir);

                // Sort in descending order
                $b = scandir($dir,1);

                print_r($b);

since I did the testing with other folder names also so it returns in html as below:: file getting created Array ( [0] => 1213121 [1] => 12131 [2] => 1213 [3] => .. [4] => . )

Also I did the testing with permissions as 0755, 0700 but none is working.


Solution

  • if your server folder permission is ok then this code is work for you. this first script for deleting the '1213121' folder from your server. script 1:

    delete_files('/public_html/members/1213121/');
    
    /* 
     * php delete function that deals with directories recursively
     */
    function delete_files($target) {
        if(is_dir($target)){
            $files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
    
            foreach( $files as $file ){
                delete_files( $file );      
            }
    
            rmdir( $target );
        } elseif(is_file($target)) {
            unlink( $target );  
        }
    }
    ?>
    

    replace your script with this script 2:

    $dir = 'public_html/members/1213121';
    
    if (!file_exists($dir) && !is_dir($dir)) { //check dir is not exist
    
        if (mkdir($dir, 0777, true)) { //check folder is created
            echo "Folder created"; //display success message
        } else {
            echo "folder not created."; //if the dir is not created then show error message
        }
    }
    
    /**this is the code I put to scan the members folder and it retuns array and showing the folder named 1213121 but in actual cpanel that directory is not there **/
    
    $dir = "public_html/members/";
    
    // Sort in ascending order - this is default
    $a = scandir($dir);
    
    // Sort in descending order
    $b = scandir($dir, 1);
    
    print_r($b);
    

    Note: before replacing 2nd script you must remove e first script.