display all images in a directory......
Moderator: General Moderators
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
display all images in a directory......
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.
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 />";
}
}
}
}
?>have fun.
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
}
}
}
}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);
}
?>- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
i ended up using this
i changed it so rather then echo-ing a link it shows the images in the folder.
thanks for all the help
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>";
}
}
}
?>thanks for all the help
As stated in the PHP.net manual, you should use
instead of
Code: Select all
while (false !== ($files = readdir($handle))) {Code: Select all
while($files = readdir($handle))- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
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....
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>";- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
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>";
}
}
}
?>The DuFF has spoken
anyway here's something that'll boost you a little to where you want to go:
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
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);
?>-Nay