Current location: Hot Scripts Forums » General Community » Script Requests » List Everything In Folder (PHP)


List Everything In Folder (PHP)

Reply
  #1 (permalink)  
Old 03-06-06, 05:05 PM
IKG IKG is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation List Everything In Folder (PHP)

I need a simple script that lists all the files in a specific folder. For example, I have a folder on my website filled with pictures. I need a script that will list all the names of the files/pictures in my folder since my folder is not public.

If someone could make this, I would be extremely grateful!

Thanks



P.S. If something simple like this already exists, please provide a link if possible.

Last edited by IKG; 03-06-06 at 05:07 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-06-06, 06:32 PM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by IKG
I need a simple script that lists all the files in a specific folder. For example, I have a folder on my website filled with pictures. I need a script that will list all the names of the files/pictures in my folder since my folder is not public.

If someone could make this, I would be extremely grateful!

Thanks



P.S. If something simple like this already exists, please provide a link if possible.
Code:
$dirname = "pathtoyourfolder/";
	
$dir = opendir($dirname);
	
while(false != ($file = readdir($dir)))
	{
	if(($file != ".") and ($file != ".."))
		{
		echo($file);
		}
	}
The script above will list all the file names in a folder, I guess if you have pictures you wont want the .jpg to appear after it, you will have to substr() it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-06-06, 06:37 PM
IKG IKG is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Wow! Thanks so much!

Now how would I implement this?


I've made a .php file and placed it in the specified folder. This is what I have so far:

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>test</title>
</head>

<body>

</body>

<?php
$dirname = "/images/personal/endicott/";
	
$dir = opendir($dirname);
	
while(false != ($file = readdir($dir)))
	{
	if(($file != ".") and ($file != ".."))
		{
		echo($file);
		}
	}
?>

</html>
Update: I tried creating a HTML file with this code instead, but it still doesn't work. All I get is a blank, white, page.

Last edited by IKG; 03-06-06 at 06:54 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-06-06, 06:54 PM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by IKG
Wow! Thanks so much!

Now how would I implement this?


I've made a .php file and placed it in the specified folder. This is what I have so far:

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>test</title>
</head>

<body>

</body>

<?php
$dirname = "/images/personal/endicott/";
	
$dir = opendir($dirname);
	
while(false != ($file = readdir($dir)))
	{
	if(($file != ".") and ($file != ".."))
		{
		echo($file);
		}
	}
?>

</html>
Yeah that should work then, does it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-06-06, 06:55 PM
Barnz1986 Barnz1986 is offline
Aspiring Coder
 
Join Date: Jan 2006
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by IKG
Wow! Thanks so much!

Now how would I implement this?


I've made a .php file and placed it in the specified folder. This is what I have so far:

Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>test</title>
</head>

<body>

</body>

<?php
$dirname = "/images/personal/endicott/";
	
$dir = opendir($dirname);
	
while(false != ($file = readdir($dir)))
	{
	if(($file != ".") and ($file != ".."))
		{
		echo($file);
		}
	}
?>

</html>
Update: I tried creating a HTML file with this code instead, but it still doesn't work. All I get is a blank, white, page.
Thats strange, just try the PHP code on its own. Call the file test.php or something.

Also on the example you used above i noticed that the <?php code ?> was not in the <body> </body> tags.

<body>

<?php

//code

?>

</body>

That might be the problem.

Just try the code in a seperate file first tho. (.php)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-06-06, 07:55 PM
Vineman Vineman is offline
Wannabe Coder
 
Join Date: Dec 2005
Location: Texas
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
His problem is that he made it an HTML file.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-06-06, 08:35 PM
IKG IKG is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
No, I tried making it a .php at first but it didn't work. Now I tried again and still no luck. Just a blank white page...

By the way, even if I get this to work will I be able to make the words link to the file?

Last edited by IKG; 03-06-06 at 08:40 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-06-06, 10:12 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
It is highly likely that the path to the directory, from the current location where the php script is running, is not correct. Your code could use some error checking (always a good idea, and certainly a good idea when debugging a problem.) Try the following to see if it is even finding and opening the directory -
PHP Code:

<?php

$dirname 
"/images/personal/endicott/";
if (
$dir opendir($dirname)) {
    while(
false !== ($file readdir($dir))) {
        if((
$file != ".") and ($file != "..")) {
            echo 
"$file<br>";
        }
    }
    
closedir($dir);
} else {
    echo 
"could not open dir: $dirname<br>";
}
?>
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-06-06, 10:20 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
It took me a second to figure out what this meant -
Quote:
By the way, even if I get this to work will I be able to make the words link to the file?
To make the file names into clickable links, change the echo statement in the middle of the loop to -
PHP Code:

echo "<A href='$dirname$file'>$file</A><br>"
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 03-14-06, 04:48 PM
IKG IKG is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
@ Mab: I tried what you suggested and got the "could not open" error listed. I also tried making it just "endicott/" but no luck...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP multi-dimensional array sorting issue aqw PHP 2 06-25-05 12:09 AM
Drop down list change PHP ancdy PHP 5 02-10-05 06:33 AM
Create List with PHP ...? demnos PHP 6 08-13-04 03:40 PM
edit php in Squirrelmail jrcortrightiii PHP 1 07-10-03 03:08 PM


All times are GMT -5. The time now is 08:11 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.