Page 1 of 1

Open folders

Posted: Wed May 13, 2009 4:13 pm
by manRay
I have a folder on my site which has more folders inside. How can I view all those folders...I have script that views all files from a folder.

Code: Select all

<?php
$dir = "../users/$email/videos";
 
$dh = opendir($dir);
 
while (false != ($file = readdir($dh))) {
        $ext = strrchr($file, '.');
    
        if($ext != false) {
        $newfile = substr($file, 0, -strlen($ext)); } 
        
        if ($file != "." && $file != "..") {
        echo "<p align='left'><a href='$dir$file'>$newfile</a></p>"; } }
        
        closedir($dh);
?>
 

Re: Open folders

Posted: Wed May 13, 2009 5:10 pm
by requinix
Recursion is your friend.

Re: Open folders

Posted: Wed May 13, 2009 5:28 pm
by manRay
how would I view a folder instead of a file?

Re: Open folders

Posted: Wed May 13, 2009 6:07 pm
by requinix
I imagine there is some way to check whether something is a directory or not.
Only print the ones that are.

Re: Open folders

Posted: Wed May 13, 2009 6:09 pm
by manRay
This is what I am working with, it is not working, so I guess I am doing something wrong.How can I fix this, to make it work?

Code: Select all

<?php
$dir = "../users/$email/";
 
$dh = opendir($dir);
 
while (false != ($folder = readdir($dh))) {
        if (folder=='$dir'."audio/")
        $pic2 = "../images/audio.gif";
        else if (folder=='$dir'."desktop/")
        $pic2 = "../images/apps.gif";
        else if (folder=='$dir'."documents/")
        $pic2 = "../images/docs.gif";
        else if (folder=='$dir'."photos/")
        $pic2 = "../images/photos.gif";
        else if (folder=='$dir'."video/")
        $pic2 = "../images/video.gif";
        
        echo "<img src=$pic2 onclick='openFolder($dir);'>";  }
        
        closedir($dh);
?>
 

Re: Open folders

Posted: Wed May 13, 2009 9:38 pm
by requinix

Code: Select all

if (folder=='$dir'."audio/")
First, "folder" is a variable. PHP 101 taught you that all variables have a dollar sign in front.
Second, variables don't work inside single-quoted strings. Just leave it as $dir, no quotes.
Third, print out each $folder and compare it with what you expect it to be. You'll find two things different.

Re: Open folders

Posted: Wed May 13, 2009 10:57 pm
by manRay
This is the code I got to work 70%.

Code: Select all

<?php
$dir = "../users/$email/";
 
$dh = opendir($dir);
 
while (false != ($folder = readdir($dh))) {
        if ($folder=='$dir/audio')
        $pic2 = '../images/audio.gif';
        else if ($folder=='$dir/desktop')
        $pic2 = '../images/apps.gif';
        else if ($folder=='$dir/documents')
        $pic2 = '../images/docs.gif';
        else if ($folder=='$dir/photos')
        $pic2 = '../images/photos.gif';
        
        
        echo "<img src='$pic2' onclick='openFolder('$dir$folder');' />";}
        
        closedir($dh);
?>
 

Re: Open folders

Posted: Wed May 13, 2009 11:09 pm
by requinix
More like 25%.
tasairis wrote:Second, variables don't work inside single-quoted strings. Just leave it as $dir, no quotes.
Third, print out each $folder and compare it with what you expect it to be. You'll find two things different.
(That last one counts for two.)

Re: Open folders

Posted: Wed May 13, 2009 11:20 pm
by manRay
printing out what $folder was really helped. It wasn't what I had expected.

Re: Open folders

Posted: Thu May 14, 2009 3:40 pm
by manRay
I am trying to get the name of a file so I can link to it later, but when I perform the function, it is not recognizing the spaces.

i.e. the test song
when I use this code \/\/\/
false != ($item = readdir($dh)))
it sets $item == the