Jump to content

[SOLVED] Display all items in a directory


adam291086

Recommended Posts

This is the code which i am working, it reads the file and displays it, it is still not complete. Maybe you can modify it and use... (and later share with me  ;D)

<?php
$dir = "homio/";
$i=0;
if ($handle = opendir($dir)) 
{
while (false !== ($file = readdir($handle))) 
{
$all_files[$i] = $file;
$i++;
}
    closedir($handle);
}

foreach ($all_files as $value) 
{
$path = $dir.$value;
print_r($value);
echo '<img src="'.$path.'" width="250" height="150"><br />';

}
?>

Link to comment
Share on other sites

Here's the code I've developed to show it in a listbox.

 

The only thing I've got to fix is stopping it showing the full directory location and just show the file titles in the list box - not sure if anyone can help me with this?

???

<select>
<?php
$ImageDirectory = "../Content/Reviews/images";
foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
echo "<option>".$image."</option>";
} 
?>
</select>

 

Link to comment
Share on other sites

I could not remove that DOTS... I switched to Delphi's code, i found it more efficient... does the same thing, just modify TR and TD's to get the look as you want... 

 

<?php
$images = "images";
foreach (glob("$images/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
echo "<table><tr>";
echo '<td><img src="'.$image.'" width="250" height="150"></td>';
echo "</tr></table>";
} 
?>

Link to comment
Share on other sites

ok i have modified the code

 

<?php
error_reporting(E_ALL);
$ImageDirectory = "../adam/cycling/admin/upload/pictures/thumbnails/";
foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
echo "<html>";
echo "<body>";
echo "<table><tr>";
echo '<td><img src="'.$image.'" width="250" height="150"></td>';
echo "</tr></table>";
echo "</html>";
echo "</body>";
} 
?>

 

Nothing is being echoed and i get no errors. Any ideas?

Link to comment
Share on other sites

verify you are getting data first


<?php
error_reporting(E_ALL);
$ImageDirectory = "../adam/cycling/admin/upload/pictures/thumbnails/";
$files = glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE);
print_r($files);
?>

If taht returns an empty array you know your $ImagDirectory isn't right, try $_SERVER['DOCUMENTS_ROOT']."LOcATION"

Link to comment
Share on other sites

ok i have changed to

 

<?php
error_reporting(E_ALL);
$ImageDirectory = dirname(__FILE__)."/pictures/thumbnail/";
$files = glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE);
print_r($files);


?>

 

i know the direct is correct as that is where my upload script uploads the images to. All that's printed is array()

Link to comment
Share on other sites

This function will do the following,

 

Select only the filetypes you specify.

Sort the files according to filetype

Show the image as a thumbnail

 

And it is easy to use

 

<?php
function show_images($directory) {
	$images_types = array('gif','jpg','png');
	foreach($images_types as images_type) {
		$images_files = glob($directory . "*." . $images_type);
		if($images_files != '') {
			foreach($images_files as $images_file) {
				print '<img src=' . $directory . $images_file . ' width=100px height=100px /><br />';
			}
		}
	}
}
$images = show_images($_SERVER['DOCUMENT_ROOT']);
if($images != '') {
	print $images;
}
?>

Link to comment
Share on other sites

ok i am using mr_mind code. The only thing echoed is


. I am confuse with the code. Any help appreciated.

 

<?php
function show_images($directory) {
	$images_types = array('gif','jpg','png');
	foreach($images_types as $images_type) {
		$images_files = glob($directory . "*." . $images_type);
		if($images_files != '') {
			foreach($images_files as $images_file) {
				print '<img src=' . $directory . $images_file . ' width=100px height=100px /><br />';
			}
		}
	}
}
$images = show_images($_SERVER['DOCUMENT_ROOT']."/pictures/thumbnails/");
if($images != '') {
	print $images;
}
?>

Link to comment
Share on other sites

ok my script has stopped working.

 

<?php
$dir = dirname(__FILE__)."/pictures/thumbnail";
foreach (glob("$dir/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{

echo '<img src="'.$image.'" width="250" height="150">';
echo '<br />';

} 
?>

 

When i run the script  it show 4 images boxes. Which is correct. The only problem is that the image itself is not being echoed. It just shows a red cross.

 

http://adamplowman.co.uk/cycling/admin/upload/view_pics.php view whats happening here

Link to comment
Share on other sites

finally got it working. The code below shows how. How would i go about echoing the file name as well?

 

<?php
$ImageDirectory = "../upload/pictures/thumbnail/";
foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
echo "<table><tr>";
echo '<td><img src="'.$image.'"></td>';
echo '<td>$ImageDirectory</td>';
echo "</tr></table>";
} 
?>

Link to comment
Share on other sites

Try this (the glob returns a complete path so you need to get just the image name)

<?php
$ImageDirectory = "../upload/pictures/thumbnail/";
foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
echo "<table><tr>";
echo '<td><img src="'.$image.'"></td>';
echo '<td>$ImageDirectory</td>';
$temp = explode("/",$image);
echo "<td>".$temp[count($count)-1]."</td>";
echo "</tr></table>";
} 

that should get you just the last item in the array exploded by the / removing all the folder contexes

Link to comment
Share on other sites

i did it the less complicated way, which i understand

 

<?php
$ImageDirectory = "../upload/pictures/thumbnail";
foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
//remove .. for url
$dir = str_replace("..", "", "$ImageDirectory");

//get name for url
$name = str_replace("$ImageDirectory", "", "$image");

//get name on it's own
$fullname = str_replace("/", "", $name);


echo '<br />';
echo $fullname;
echo '<br />';
echo '<img src="'.$image.'">';
echo '<br />';
echo "www.adamplowman.co.uk".$dir.$name;
echo '<br />';




}


?>

Link to comment
Share on other sites

yeah if you want that full path a better idea your way, that or what makes more sense (a universal one is)

<?php
$ImageDirectory = "../upload/pictures/thumbnail";
foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image)
{
$path = str_replace($_SERVER['DOCUMENT_ROOT'],"",$image);
echo '<br />';
echo $fullname;
echo '<br />';
echo '<img src="'.$image.'">';
echo '<br />';
echo $path;
echo '<br />';




}


?>

That will work on any server instead of only yours, and its simpler

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.