0

i'd been trying to make a project which is a simple file manager that list all files and allows you to do everything that a file manager does , so I first wrote the below script for listing the files which was the initial phase , I ran the below script on my server and it was working fine the scripts are :

<?php
if ( $handle = opendir('/home1/myusername') )
{
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    /* This is the correct way to loop over the directory. */
    while ( false !== ( $entry = readdir( $handle ) ) )
    {
        echo "$entry\n";
    }

}
?>

but when I change the directory to just "/home1" it wouldn't list any files I mean when i make the below change :

<?php
    if ( $handle = opendir('/home1') )
    {
        echo "Directory handle: $handle\n";
        echo "Entries:\n";
    
        /* This is the correct way to loop over the directory. */
        while ( false !== ( $entry = readdir( $handle ) ) )
        {
            echo "$entry\n";
        }
    
    }
    ?>

while when I go in this directory via ssh putty , and do ls , I get my username directory ..

enter image description here

i am just wondering why opendir() failed to do so ? and is there any solution or any other function which i can use in replace to that ?

Umair Ali
  • 31
  • 6
  • Do you have error reporting enabled? That should throw a notice/error if it is a permissions issue. Do the two directories have the same permissions? Are you executing through browser or CLI? If CLI as root or as `myusername`? – user3783243 Dec 14 '21 at 11:25
  • @user3783243 I am executing it as myusername both in ssh putty also in php script and not as root , connecting with ssh putty , and then executing the "ls" command in /home dir shows me my username folder, but when I write the same directory in the opendir function it shows me nothing – Umair Ali Dec 14 '21 at 11:32
  • Error reporting is enabled? – user3783243 Dec 14 '21 at 11:33
  • well I guess so because , I notice there was a error_log file generated when I was working on another script , but it didn't generated for this script – Umair Ali Dec 14 '21 at 11:36
  • Please add https://stackoverflow.com/a/21429652/3783243 to make sure it is enabled. – user3783243 Dec 14 '21 at 11:39
  • @user3783243 oh yes , permission denied , but why was I able to list in /home dir with ssh via putty? – Umair Ali Dec 14 '21 at 12:02
  • Not sure I follow the behavior. Can you please edit the question with what you do that works and what you do that doesnt? – user3783243 Dec 15 '21 at 23:17
  • @user3783243 but Its already in the question , that I tried with ssh , it listed the files , but doesn't list if I put the same path in the opendir() function .? – Umair Ali Dec 15 '21 at 23:27
  • You are running `php file.php` which contains `if ( $handle = opendir('/home1') )`? as the same user that ran the `ls`? – user3783243 Dec 16 '21 at 00:34
  • @user3783243 exactly yes ! – Umair Ali Dec 16 '21 at 00:37

0 Answers0