Forum Moderators: coopster

Message Too Old, No Replies

how do i find out what files is in a folder and how many files

how do i find out what files is in a folder and how many files

         

MrGecko

10:46 pm on Nov 14, 2006 (gmt 0)

10+ Year Member



how do i find out what files is in a folder and how many files there are
because i'm making a php photo album with pictures in a folder and i want it to find out what is in the folder and display it

i know how to find out how many there are with count()
i know how to display it if it's like this name[1]

so if you can so me how to make it put the name of each file in name[1] name[2]
than that is all i need to know

dreamcatcher

11:26 pm on Nov 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi MrGecko,

First, loop through your folder and assign images to an array:

$pictures = array();

$folder = opendir("pictures/");

while ($read = readdir($folder))
{
if ($read!= "." && $read!= "..")
{
$pictures[] = $read;
}
}

closedir($folder);

There are <?php echo count($pictures);?> pictures.

Then use a for loop for retrieve them. You can also use a foreach loop if you want, but for loops are more fun. :)


if (!empty($pictures))
{

for ($i=0; $i<count($pictures); $i++)
{
echo $pictures[$i]."<br>";
}

}

Or you can access them individually. Only recommended if you know how many there are or else you will get a undefined offset error.

echo $pictures[1];
echo $pictures[2];

Finally, view the contents of your array like this:

print_r($pictures)

dc

eelixduppy

11:28 pm on Nov 14, 2006 (gmt 0)



glob [us3.php.net] is a very useful function:

$path = "directory/path";
$files = glob($path.'/*.*');
echo '<pre>';
print_r($files);
echo '</pre>';
echo '<br/><br/>';
$num_files = count($files);
echo 'There are '.$num_files.' in this directory: '.$path;

If you don't like this solution, take a look at readdir [us3.php.net].

Good luck :)

[edit]
Seems like I'm slowing down on my typing ;)
[/edit]

MrGecko

4:04 pm on Nov 15, 2006 (gmt 0)

10+ Year Member



thanks eelixduppy i'm useing yours but it dose not display them in order
it go's like this
1.jpg
10.jpg
11.jpg
if someone can make it work in order of
1.jpg
2.jpg
3.jpg
that will be better
thanks for the help

eelixduppy

7:56 pm on Nov 15, 2006 (gmt 0)



You can always sort [us2.php.net] the array.

alfaguru

8:32 pm on Nov 15, 2006 (gmt 0)

10+ Year Member



You need to use a natural sort (natsort()) to get numbers in order of size.

eelixduppy

8:35 pm on Nov 15, 2006 (gmt 0)



Good catch, alfaguru. Thanks ;)

MrGecko

2:35 pm on Nov 16, 2006 (gmt 0)

10+ Year Member



with sort(); and sort(natsort());
both did not work this is the output
Pictures-Thumbnails/0.jpg
Pictures-Thumbnails/1.jpg
Pictures-Thumbnails/10.jpg
Pictures-Thumbnails/11.jpg
...

it is not outputting in order
can some make it work
this is my script
$file = glob("Pictures-Thumbnails".'/*.*');
sort (natsort($file));

coopster

4:04 pm on Nov 16, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Don't sort [php.net] the natsort again, you are resetting it! OT: Some side notes on the use of glob ->
[webmasterworld.com...]

MrGecko

4:42 pm on Nov 16, 2006 (gmt 0)

10+ Year Member



nope did not work natcasesort(); natsort(); sort(); none worked
it is still in the same order

MrGecko

6:10 pm on Nov 16, 2006 (gmt 0)

10+ Year Member



here is my code
<?
$do = isset($_REQUEST['do'])? $_REQUEST['do'] : 1;
switch ($do) {
case "Picture":
$file = glob("Pictures-Images/*.jpg");
natsort($file);
$Picture = isset($_REQUEST['Picture'])? $_REQUEST['Picture'] : 1;
?>
<html>
<head>
<title>Pictures</title>
<?=$style?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<?
if ($Picture==0) {
$next = $Picture + 1;
echo "<br>Back <a href=\"javascript:window.close();\">Close</a> <a href=\"$PHP_SELFE?do=Picture&Picture=$next\">Next</a><br>";
} elseif ($Picture==count($file)-1) {
$back = $Picture - 1;
echo "<br><a href=\"$PHP_SELFE?do=Picture&Picture=$back\">Back</a> <a href=\"javascript:window.close();\">Close</a> Next<br>";
} else {
$back = $Picture - 1;
$next = $Picture + 1;
echo "<br><a href=\"$PHP_SELFE?do=Picture&Picture=$back\">Back</a> <a href=\"javascript:window.close();\">Close</a> <a href=\"$PHP_SELFE?do=Picture&Picture=$next\">Next</a><br>";
}
?>
<br>
<img src="<?=$file[$Picture]?>">
</body>
</html>
<?
break;
default:
$file = glob("Pictures-Thumbnails/*.jpg");
natsort($file);
$jmlrec = count($file);
$page = isset($_REQUEST['page'])? $_REQUEST['page'] : 1;
$max_entry_per_page = 10;
?>
<html>
<head>
<title>Pictures</title></head>
<body>
<?
$jml_page = intval($jmlrec/$max_entry_per_page);
$sisa = $jmlrec%$max_entry_per_page;
if ($sisa > 0) $jml_page++;
$no = $page*$max_entry_per_page-$max_entry_per_page;
if ($jmlrec == 0) echo "<tr><td colspan=\"3\" align=\"center\"><font size=\"3\">Not found.</font></td></tr>";
if ($jml_page > 1) {
if ($jml_page > 10) {
if ($page < 5) {
$start = 1;
$stop = 10;
} elseif ($jml_page - $page < 5) {
$start = $jml_page - 9;
$stop = $jml_page;
} else {
$start = $page-4;
$stop = $page+5;
}
if ($start <> 1) echo "... ";
for ($p=$start; $p<=$stop; $p++) {
if ($p == $page) echo "<font color=\"$active_link\"><b>Page $p</b>&nbsp;</font>";
else echo "<a href=\"$PHP_SELF?page=$p\">Page $p</a> ";
}
if ($stop <> $jml_page) echo "... ";
} else {
for ($p=1; $p<=$jml_page; $p++) {
if ($p == $page) echo "<font color=\"$active_link\"><b>Page $p</b>&nbsp;</font>";
else echo "<a href=\"$PHP_SELF?page=$p\">Page $p</a>&nbsp;";
}
}
}
$w = 0; //--Color
for ($i=0; $i<$max_entry_per_page; $i++) {
if (isset($file[$no])) {
?><br><a href="<?=$PHP_SELFE?>?do=Picture&Picture=<?=$no?>" target="_blank"><img src="<?=$file[$no]?>"></a>
<?
$no++;
}
}
if ($jml_page > 1) {
if ($jml_page > 10) {
if ($page < 5) {
$start = 1;
$stop = 10;
} elseif ($jml_page - $page < 5) {
$start = $jml_page - 9;
$stop = $jml_page;
} else {
$start = $page-4;
$stop = $page+5;
}
if ($start <> 1) echo "... ";
for ($p=$start; $p<=$stop; $p++) {
if ($p == $page) echo "<font color=\"$active_link\"><b>Page $p</b>&nbsp;</font>";
else echo "<a href=\"$PHP_SELF?page=$p\">Page $p</a> ";
}
if ($stop <> $jml_page) echo "... ";
} else {
for ($p=1; $p<=$jml_page; $p++) {
if ($p == $page) echo "<font color=\"$active_link\"><b>Page $p</b>&nbsp;</font>";
else echo "<a href=\"$PHP_SELF?page=$p\">Page $p</a>&nbsp;";
}
}
}
?>
</body>
</html>
<?
break;
}

[edited by: MrGecko at 6:15 pm (utc) on Nov. 16, 2006]

coopster

4:26 pm on Nov 20, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



print_r [php.net] is your friend here. It will dump the contents of an array in a nice, readable format if you enclose it in HTML <pre> elements. First, make sure your array is coming out in the order you like by dumping it to the browser. Note the inserted code in bold here ...
<? 
$do = isset($_REQUEST['do'])? $_REQUEST['do'] : 1;
switch ($do) {
case "Picture":
$file = glob("Pictures-Images/*.jpg");
natsort($file);
print '<pre>'; print_r($file); print '</pre>'; exit;
$Picture = isset($_REQUEST['Picture'])? $_REQUEST['Picture'] : 1;
?>

If this looks correct, then somewhere in your code you are altering your logic to display the results in a different order.