Jump to content

Open directory get images, and display


peranha

Recommended Posts

I want to open a directory, and get all the images in that folder and display them to the webpage  No databse, just a directory of pics.  Here is what I have.

 

getimage.php page.

<?php
$path = "C:/pics";
//images them dir
//$patha ="C:/pics";  
$dh = opendir($path);
while ($file = readdir($dh)) {
if($file != "." && $file != "..") {
	$extension = substr($path, -3); 
	if($extension == "jpg" || $extension == "jpeg"){ 
		header("Content-type: image/jpeg"); 
	}elseif($extension == "gif"){ 
		header("Content-type: image/gif"); 
	}elseif($extension == "bmp"){
		header("Content-type: image/bmp");
	}
	$path = "$path/$file";
	readfile($path);
}
}
closedir($dh); // close dir
?>

 

Here is how it is called on the page.

 

images1.php

<?php
echo "<img src='getimage.php' alt='' width=100 />";
?>

 

Currently it gets the first image in the directory, but every image after that, it adds the first image file name to the $path variable.

 

example

img1 = P3140722.JPG

img2 = P3140722.JPG/P3140723.JPG

img3 = P3140722.JPG/P3140723.JPG/P3140724.JPG

etc....

 

Any ideas?

Link to comment
Share on other sites

Here is something that I use to display all the pictures from a folder on my server. Hope this helps. Just change the $imgPath to what your folder configuration is, and change to picture extensions if you want

<?php 
$imgPath = "userimages/Computers";
foreach(glob("$imgPath/{*.jpg,*.png,*.gif}", GLOB_BRACE) as $fname)
{
echo '<img src="' . $imgPath . '/' . baseName($fname) . '" width="150" border="1"><br/><br/>'; 
}
?>

Link to comment
Share on other sites

While is a loop. You're storing $path/$file back into $path each time it loops back over but not resetting $path back to it's original value. Which explains why it just keeps adding $file to the end of $path.

 

To fix this, either reset $path or create another variable for storing the full path:

 

Resetting path:
$path = "$path/$file";
readfile($path);
$path = "C:/pics";

Using new variable:
$full_path = "$path/$file";
readfile($full_path);

 

Or don't even set $path in the while loop and put the $path/$file directly in readfile:

 

replace:
$path = "$path/$file";
readfile($path);

with:
readfile("$path/$file");

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.