Open folders

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Open folders

Post 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);
?>
 
Last edited by Benjamin on Wed May 13, 2009 4:58 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Open folders

Post by requinix »

Recursion is your friend.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Open folders

Post by manRay »

how would I view a folder instead of a file?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Open folders

Post by requinix »

I imagine there is some way to check whether something is a directory or not.
Only print the ones that are.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Open folders

Post 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);
?>
 
Last edited by Benjamin on Wed May 13, 2009 6:10 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Open folders

Post 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.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Open folders

Post 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);
?>
 
Last edited by Benjamin on Wed May 13, 2009 11:53 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Open folders

Post 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.)
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Open folders

Post by manRay »

printing out what $folder was really helped. It wasn't what I had expected.
manRay
Forum Commoner
Posts: 78
Joined: Mon Feb 09, 2009 1:57 pm

Re: Open folders

Post 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
Post Reply