/    Sign up×
Community /Pin to ProfileBookmark

function to find the contents of a dir

is there a function that returns the contents of a directory on your server? I need something like this to make a script where one can up a butt load of pics by ftp and run this script to make an html page that has the source for a photo album.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 10.2005 — Start reading here: http://www.php.net/readdir ?
Copy linkTweet thisAlerts:
@bokehMay 10.2005 — [code=php]
<?php

$dir = '/path/to/image/directory/from/root/';

//get directory contents
$contents = array();

$dir = opendir($_SERVER['DOCUMENT_ROOT'] . $dir);

while (false !== ($file = readdir($dir))) {
$contents[] = $file;
}
closedir($dir);



// Print file names
foreach($contents as $file){
if(($file != '.') and ($file != '..')){
print ($file . '<br>');
}
}

?>
[/code]


Or the following to display them

[code=php]
<?php

$dir = '/path/to/image/directory/from/root/';

//get directory contents
$contents = array();

$dir = opendir($_SERVER['DOCUMENT_ROOT'] . $dir);

while (false !== ($file = readdir($dir))) {
$contents[] = $file;
}
closedir($dir);

//get only jpeg contents
$jpeg_contents = array();

foreach($contents as $file){
if (eregi('.jpg{1}$', $file)){
$jpeg_contents[] = $file;
}
}

// display images
foreach($jpeg_contents as $file){
print ('<img src="/http/path/to/directory/' . $file . '"><br>' . "n");
}

?>
[/code]


Edit: You could scale all that down to something like the following:

[code=php]
<?php
$dir = '/path/to/image/directory/from/root/';

$dir = opendir($_SERVER['DOCUMENT_ROOT'] . $dir);

while (false !== ($file = readdir($dir))) {
if (eregi('.jpg{1}$', $file)){
print ('<img src="/http/path/to/image/directory/' . $file . '"><br>' . "n");
}
}
closedir($dir);
?>
[/code]
Copy linkTweet thisAlerts:
@gameguy43authorMay 11.2005 — good help both, thank you!
×

Success!

Help @gameguy43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...