简体   繁体   中英

Why do scandir, opendir and glob all NOT return my directory contents

I'm currently in the process of creating an application to generate and manage project names based on predefined themes. This application features very basic cloud saving functionality. It's super simple and designed to work without a database by saving the generated save data on files on a server.

In order for the program to download all the saved files I need to list all the saved files in a folder on the server. However, I can't seem to get the expected response from my server. I've tried 3 different ways to list all the files, and NONE of them return any files, which seems very odd to me.

$dir = "WordPress_SecureMode_01/Bubba/";

echo pathinfo($dir, PATHINFO_DIRNAME);

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}

$files = scandir('WordPress_SecureMode_01/Bubba/');

foreach($files as $file){

    echo $file;
    echo pathinfo($file, PATHINFO_FILENAME);
}

$entries = glob('WordPress_SecureMode_01/Bubba/*.txt');

foreach($entries as $entry){

    echo $entry;
}

As you can see I'm now using three different methods of retrieving the files. opendir, scandir and glob. All their findings are echoed and thus retrieved by my application. However, the only data my application receives is the output of the pathinfo method at the top of the script. So, the communication between client and server is working fine, but all the options for scanning directory files aren't.

Does anyone have an idea as to why this behaviour is ocurring?

Here in your code $dir is not a directory and that's the reason the code isn't moving forward.

Please check the path,whether it is dir or not by simply executing

$dir = "WordPress_SecureMode_01/Bubba/";

if (is_dir($dir)) {
echo 'Yes';
}
else{
echo 'No';
}

If it gives you Yes it means its not about path and if it returns you No then please change your path to that folder.

You either want to use an absolute path, if the files are in the same directory:

$dir = __DIR__;

Or a relative path, if they are in the same directory:

$dir = "./";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM