Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,172 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,238 people online right now. Registration is fast and FREE... Join Now!




How to display all files in a directory?

 
Reply to this topicStart new topic

How to display all files in a directory?, php newbie

jamessy
25 Aug, 2008 - 10:08 AM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 25


My Contributions
Hi folks need some help ! i basically have an admin page which is able to add text/title and images to certain pages
and all i want to do is to output the all contents of this folder(images) on to a webpage, what php method suits this best ?
User is offlineProfile CardPM
+Quote Post

grimpirate
RE: How To Display All Files In A Directory?
25 Aug, 2008 - 11:01 AM
Post #2

D.I.C Head
Group Icon

Joined: 3 Aug, 2006
Posts: 227



Thanked: 6 times
Dream Kudos: 375
My Contributions
Take a look at the scandir function.
User is offlineProfile CardPM
+Quote Post

jamessy
RE: How To Display All Files In A Directory?
25 Aug, 2008 - 10:58 PM
Post #3

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 25


My Contributions
Thanks for that very grateful ! right now ive sortoff got it working the only problem i have now is that i want to display each image once but for some reason its displaying all images times how many images are in the folder.Heres the code

CODE


$folder= 'C:/wamp\www/mytemp/uploads/';
$files = scandir($folder);
var_dump($files);
for($i=0;$i<sizeof($files); $i++){


echo '<img src ="uploads/'.$files[$i].'"/>';

}



any help would be grateful
cheers
User is offlineProfile CardPM
+Quote Post

musya
RE: How To Display All Files In A Directory?
25 Aug, 2008 - 11:03 PM
Post #4

D.I.C Regular
Group Icon

Joined: 25 Apr, 2007
Posts: 296



Thanked: 1 times
Dream Kudos: 50
My Contributions
try doing a var_dump after the loop, but other than that i cant see anything wrong with it at this point, maybe someone more experienced would be able to help you out more than I.
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: How To Display All Files In A Directory?
26 Aug, 2008 - 03:01 AM
Post #5

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
Try this:

CODE

<?php
$folder= 'C:/wamp\www/mytemp/uploads/';
$files = @opendir($folder) or die("Unable to open $folder");

while ($file = readdir($files )) {

echo '<img src ="uploads/'.$file].'"/><br />';

}
?>

User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: How To Display All Files In A Directory?
26 Aug, 2008 - 07:32 AM
Post #6

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
On a Mac, PHP won't understand ".", "..", and ".DS_Store", so I use:
CODE
<?php
$folder= "images";
$files = scandir($folder);
var_dump($files);
echo "<br>";
for($i=3;$i<sizeof($files); $i++){
echo '<img src = "'.$folder."/".$files[$i].'" />';
echo "<br>"."<br>";
}
?>



User is offlineProfile CardPM
+Quote Post

jamessy
RE: How To Display All Files In A Directory?
26 Aug, 2008 - 11:36 AM
Post #7

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 25


My Contributions
Hi thanks for the help but its still showing the whole content per amount of images, apparently i have to look into output buffering now which sounds like its over my head but il look into to it and let you know if it helps resolve my problem
cheers
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:21AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month