Page 1 of 2
display all images in a directory......
Posted: Sun Dec 14, 2003 8:37 pm
by dull1554
My goal at the end of my current project is to be able to have a acript that will let you upload a file, then the script takes the file creates a thumbnail of it, i want to then have it display all the images in a given directory, and this is where i hit a road block, i can do everything but this, i know that this is not the best place to ask this question but i would really like some help, if anything a suggestion as to where to look to find a answer, i've allready looked at the php manual and i've googled it dead......any help would be greatly appreciated.
Posted: Sun Dec 14, 2003 8:43 pm
by qads
Code: Select all
<?php
function menu()
{
$folder = "content";
if($handle = opendir($folder))
{
while($files = readdir($handle))
{
if ($files != "."
&& $files != ".."
&& $files != "Log Out.inc.php"
&& $files != "favelink.inc.php"
)
{
$files = ucwords($files);
$files = substr($files,0,-8);
$showfile = eregi_replace("_"," ",$files);
echo "<a href="main.php?module=$folder&load=$files" title="$showfile">".$showfile."<br />";
}
}
}
}
?>
the above code will read a dir and get the names of the files from it, you can filter any files u dont want to show in there too, i could edit it for you but where is the fun for you in that?
have fun.
Posted: Sun Dec 14, 2003 8:50 pm
by dull1554
thanks that gives me the start that i need, and it is unquestionable that i will have a blast.....i love making money.....lol......thanks
Posted: Sun Dec 14, 2003 8:53 pm
by dull1554
could you possiably go a bit farther to explain what does what? that would be appreciated, i dont want to just take it and use your code, i want to be able to understand it and be able to apply it in other applications
Posted: Sun Dec 14, 2003 9:17 pm
by DuFF
Code: Select all
function menu()
{
$folder = "content"; //folder name
if($handle = opendir($folder)) //open the folder
{
while($files = readdir($handle)) // $files will loop through every file in the directory
{
if ($files != "." && $files != "..") // readdir returns "." and ".." but they arent' actual files, so strip them out
{
$files = ucwords($files); //add uppercase to the first letter of all files
$files = substr($files,0,-8); //strip off all but the first 8 chars of the file
$showfile = eregi_replace("_"," ",$files); //replace all spaces with "-"'s
echo "<a href="main.php?module=$folder&load=$files" title="$showfile">".$showfile."<br />"; //output a link to the file
}
}
}
}
Here is how PHP.net's example, they say that while($files = readdir($handle)) is the wrong way to loop over the directory:
Code: Select all
<?php
$folder = "/path/to/files";
if ($handle = opendir('$folder')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
This will loop through all the files and show their filename. Just edit it to however you want it.
Posted: Sun Dec 14, 2003 9:31 pm
by dull1554
so that will output a link to a single file right, is there anyway i can add all the files to a array and then display all the files/images in the array?, and thanks for commenting it out
Posted: Sun Dec 14, 2003 9:33 pm
by Weirdan
echo is in a loop, so it will display all files
Posted: Sun Dec 14, 2003 9:41 pm
by dull1554
oh really ok thankyou
Posted: Sun Dec 14, 2003 9:52 pm
by dull1554
i ended up using this
Code: Select all
<?php
$folder = "images/"; //folder name
if($handle = opendir($folder)) //open the folder
{
while($files = readdir($handle)) // $files will loop through every file in the directory
{
if ($files != "." && $files != "..") // readdir returns "." and ".." but they arent' actual files, so strip them out
{
echo "<center><img src=".$folder.$files."></center><br>";
}
}
}
?>
i changed it so rather then echo-ing a link it shows the images in the folder.
thanks for all the help
Posted: Sun Dec 14, 2003 9:56 pm
by DuFF
As stated in the PHP.net manual, you should use
Code: Select all
while (false !== ($files = readdir($handle))) {
instead of
Posted: Sun Dec 14, 2003 10:04 pm
by dull1554
i made the changes, it works just how i want it to, i have one more question for anywho who may want to answer it. my question is can i make it so if there are more then say 10 images in the folder it will display the first ten images on the first page and add a link to a second or third or whatever page? just so the page is not a mile long, its not needed but theres no doubt in my mind that this feature would be nice.....ass usual any help would be greatly appreciated....
Posted: Sun Dec 14, 2003 10:17 pm
by Weirdan
Code: Select all
$max_per_page=10;
$start_from=$_GET['start_from'];
$folder = "images/"; //folder name
if($handle = opendir($folder)) //open the folder
{
$i=0;
while($files = readdir($handle)) // $files will loop through every file in the directory
{
if ($files != "." && $files != "..") // readdir returns "." and ".." but they arent' actual files, so strip them out
{
if($i<$start_from) continue;
elseif ($i<$start_from+$max_per_page) break;
else echo "<center><img src=".$folder.$files."></center><br>";
}
}
}
echo "<a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from-$max_per_page."'>Prev</a><a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from+$max_per_page."'>Next</a>";
Something like this....
Posted: Sun Dec 14, 2003 10:21 pm
by dull1554
so like this?
Code: Select all
<?php
echo "<center><h1>Pictures</h1></center>";
$folder = "images/"; //folder name
if($handle = opendir($folder))
{
//while($files = readdir($handle))
while (false !== ($files = readdir($handle)))
{
if ($files != "." && $files != ".." && $files != "Thumbs.db")
{
$max_per_page=10;
$start_from=$_GET['start_from'];
$folder = "images/"; //folder name
if($handle = opendir($folder)) //open the folder
{
$i=0;
while($files = readdir($handle)) // $files will loop through every file in the directory
{
if ($files != "." && $files != "..") // readdir returns "." and ".." but they arent' actual files, so strip them out
{
if($i<$start_from) continue;
elseif ($i<$start_from+$max_per_page) break;
else echo "<center><img src=".$folder.$files."></center><br>";
}
}
}
echo "<a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from-$max_per_page."'>Prev</a><a href='{$_SERVER['PHP_SELF']}?start_from=".$start_from+$max_per_page."'>Next</a>";
}
}
}
?>
Posted: Sun Dec 14, 2003 10:40 pm
by Weirdan
Look once more at what you had posted.

Posted: Mon Dec 15, 2003 3:12 am
by Nay
The DuFF has spoken

anyway here's something that'll boost you a little to where you want to go:
Code: Select all
<?php
$directory = "/home/virtual/site95/fst/var/www/html/gallery/images/";
$original = $_SERVER['QUERY_STRING'];
$original = $image_path . $original;
$new_width = "160";
$new_height = "125";
$extension = explode(".", $original);
$extension = $extension[1];
if($extension == "jpg" || $extension == "jpeg") {
$img = imagecreatefromjpeg($original);
} elseif($extension == "png") {
$img = imagecreatefrompng($original);
}
$width = imagesx($img);
$height = imagesy($img);
$temp_img = imagecreatetruecolor($new_width, $new_height);
$action = imagecopyresized($temp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$action = imagedestroy($img);
$img = $temp_img;
header("Content-type: image/jpeg");
imagejpeg($img);
?>
Errm, it's a resizer I made a few months back. mMm, there's a bunch of things lying around my /tests folder <_<. Btw, you use this a different file and do a this_file.php?nay_rocks.jpg.
-Nay