Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to read all directories of a specified directory via PHP. (unfortunately Im not good enough in VB/C/Java to make a more efficient workaround)

With this code, I could read all files from /tmp/ for example, I'm a Mac User.

<?php
$config = array(

    'dir' => '/Volumes/Data/Media-Server/Musik/Sortiert', // path to scan
    'depth' => 2, // depth of scan
    'html' => true // html output (bool)

);

if(file_exists($config['dir'])) {

    if(is_readable($config['dir'])) {

        $array = scandir($config['dir']);
        print_r($array);

    }

}
?>

But I only get a blank, white page when I try it with my network media server. You got any clues for me to get the directories?

PS: I got full r/w/x access to the server.

share|improve this question
    
Do you actually need to do something with this directory list in PHP or are you just trying to get a list of directories? If the latter why not just use find? –  Zoredache Mar 14 '12 at 18:54
add comment

2 Answers

I ran your script on my own server- It works on my tmp and home but not in a /home/user directory. So sufficed to say, your script works. What it doesn't tell me is the file permissions, so I added an else clause to your script using the fileperms method as followed:

else{
    echo substr(sprintf('%o', fileperms($config['dir'])), -4);
}

Now, when you refresh the page, you should get the octal value of the permissions.

My question to you now is, what values are returned when you refresh the script? Have you mounted the network drive with enough permissions so that the webserver is able to access it? At a minimum, the file permissions that should be printed are 0755

share|improve this answer
    
It doesn't output anything. I tried to echo something in each else-Clause, but it seems like all conditions are given (existing + readable). The strange thing is, why it doesnt write down the dir? Its connected via AFP, the Apple aquivalent to SMB. –  tqn Mar 14 '12 at 19:19
    
eh. That implies that what you have is working but it doesn't actually have the permissions to read? Copy that echo string into the is_readable if block. –  qweet Mar 14 '12 at 19:21
add comment

Your script does a couple of checks on file accessibility, but has no way of showing where it failed. I would consider adding a few simple conditions to your scripts that would output the point of failure.

IE:

<?php
$config = array(

    'dir' => '/Volumes/Data/Media-Server/Musik/Sortiert', // path to scan
    'depth' => 2, // depth of scan
    'html' => true // html output (bool)

);

if(file_exists($config['dir'])) {

    if(is_readable($config['dir'])) {
        $array = scandir($config['dir']);
        print_r($array);
    }else {
        die("Unable to read file.");
    }

}else {
    die("File not found.");
}
?>

This is a simple example, of course one could be more elaborate on the error handling but this should help you pinpoint whether the file is unreadable or not found.

Hope this helps, good-luck & happy coding.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.