...

View Full Version : PHP file to get image files in a directory..



Toups
01-22-2010, 09:40 PM
<?php



$dirname = '.';

$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)"; //valid image extensions



if($handle = opendir($dirname)) {



while(false !== ($file = readdir($handle))){

if(eregi($pattern, $file)){ //if this file is a valid image



echo '<img src="'.$file.'"/>';



}

}



closedir($handle);



}



?>


Is the code, currently it works if the images are in the same directory as the php file... however I can't get it to find images in a folder called "images" in the folder that the file is in..

I have tried changing
$dirname = '.'; to


$dirname = '/images/';
among many other things.. any help?

masterofollies
01-22-2010, 10:07 PM
Are you sure images isn't back a folder? If so you'd have to do ../images

The best way would be to do the full path, like home/php/user/public_html/images/

Toups
01-22-2010, 10:12 PM
Are you sure images isn't back a folder? If so you'd have to do ../images

The best way would be to do the full path, like home/php/user/public_html/images/



I have tried using the full path, didn't work
Tried ../images
still no luck.

folder directory is like this..:

website
--folder (php file in this folder)
-----images (images in this folder)


the images folder is inside the php file's folder.

Len Whistler
01-22-2010, 10:26 PM
The problem with the full path is when you upload to server, then the path changes and you have to edit file. To go up one folder try:

$dirname = '../images/';
or
$dirname = "../images/";

It's untested and may need some tweaking.


EDIT:
" the images folder is inside the php file's folder."

Well then $dirname = "images/"; should work, if not try single quotes.


------------

Toups
01-22-2010, 10:39 PM
The problem with the full path is when you upload to server, then the path changes and you have to edit file. To go up one folder try:

$dirname = '../images/';
or
$dirname = "../images/";

It's untested and may need some tweaking.


EDIT:

Well then $dirname = "images/"; should work, if not try single quotes.


------------

tried all your suggestions, still no luck =\

http://www.freewebsitereview.org/fader/

Has all your suggestions, each in its seperate div, only one that works is still the $dirname = '.';

Len Whistler
01-22-2010, 10:44 PM
The path to that image is:

fader/pic12.jpg

Your image folder is at: fader/images Try:

$dirname = "fader/images/";

Toups
01-22-2010, 10:48 PM
The path to that image is:

fader/pic12.jpg

Your image folder is at: fader/images Try:

$dirname = "fader/images/";


yes I know that's because that is the working one, the one that doesn't see the images folder when
$dirname is set to '.'

so yes, that image is fader/pic12.jpg

however the images folder is fader/images

$dirname = "fader/images/";
= no luck

Len Whistler
01-22-2010, 10:57 PM
website
--folder (php file in this folder)
-----images (images in this folder)

If that folder name is fader, then the correct path to images from that php file is:


$dirname = "images/"; // try single quotes if double quotes doesn't work


Is the php file and images folder in the same folder?

-------------

Toups
01-22-2010, 11:14 PM
website
--folder (php file in this folder)
-----images (images in this folder)

If that folder name is fader, then the correct path to images from that php file is:


$dirname = "images/"; // try single quotes if double quotes doesn't work


Is the php file and images folder in the same folder?

-------------
Nope,

Website
--Folder // php file located here
--Images // images located here

Screenshot of the structure:

http://www.freewebsitereview.org/fader/structure.jpg


Here's full code of the php file:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>testing</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="keywords" content="blackstone-millville, blackstone, millville, regional, school, district, education, high, middle, elementary, massachusetts, melville, milville, bmrsd ">

<meta name="description" content="Welcome to the Blackstone-Millville Regional School District. Located in Blackstone, Massachusetts U.S.A., we are a K-12 Public School System serving the towns of Blackstone and Millville">

<meta name="distribution" content="global">

<meta name="resource-type" content="document">

<meta name="revisit-after" content="3 days">





<style type="text/css">



.zind {

z-index:1;

}



body {

margin-left: 0px;

margin-top: 0px;

margin-right: 0px;

margin-bottom: 0px;

}

body,td,th {

font-family: Arial, Helvetica, sans-serif;

font-size: 12px;

color: #000000;

}

.style1 {font-family: Arial, Helvetica, sans-serif}

-->

</style>





<!-- link to magicslideshow.css file -->

<link rel="stylesheet" type="text/css" href="magicslideshow.css" media="screen"/>

<!-- link to magicslideshow.js file -->

<script src="magicslideshow.js" type="text/javascript"></script>

<script type="text/javascript">

MagicSlideshow.options = {

'speed':'4',

'effect':'fade',

'effect-next':'fade',

'effect-jump':'fade',

'loop':'yes',

'loop-type':'next',

'text-duration':'1.0'

}

</script>





</head>



<body>
















Test
Test
Test





<div class="MagicSlideshow">

<?php



$dirname = "images/";

$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)"; //valid image extensions



if($handle = opendir($dirname)) {



while(false !== ($file = readdir($handle))){

if(eregi($pattern, $file)){ //if this file is a valid image



echo '<img src="'.$file.'"/>';



}

}



closedir($handle);



}



?>

</div>



</body>

</html>

Len Whistler
01-22-2010, 11:35 PM
Is the php file and images folder in the same folder?


Nope,


According to the screen shot the php file and images folder are in the same folder. The correct path to the images folder from the index.php file is:


$dirname = "images/"; // try single quotes if double quotes doesn't work



-------

Toups
01-23-2010, 12:11 AM
According to the screen shot the php file and images folder are in the same folder. The correct path to the images folder from the index.php file is:


$dirname = "images/"; // try single quotes if double quotes doesn't work



-------

oh sorry, thought you meant the php file being in the images fodler itself, not them both being in the fader folder.

none the less,
$dirname = "images/";
does not work.

Len Whistler
01-23-2010, 12:43 AM
Try small changes such as:


$dirname = 'images';

// or

$dirname = "images";

Toups
01-23-2010, 04:00 AM
Try small changes such as:


$dirname = 'images';

// or

$dirname = "images";

still no luck =\

degroundshaker
07-31-2012, 02:22 PM
Well there was a small thing missing in your code.
As your images are in a folder called "images", you have not specified the directory while calling <img src...> tag.

So, code should be something like this now:


<?php
error_reporting(0);
$dirname = "images";

$pattern="(\.jpg$)|(\.png$)|(\.jpeg$)"; //valid image extensions



if($handle = opendir($dirname)) {



while(false !== ($file = readdir($handle))){

if(eregi($pattern, $file)){ //if this file is a valid image


echo "<img src=\"images/";
echo $file;
echo "\" width=\"100\" height=\"100\">";




}

}



closedir($handle);



}

?>


Enjoy :thumbsup:

Arcticwarrio
07-31-2012, 03:15 PM
this will do what you want:


<?php
error_reporting(0);
$config = array("images",array('.jpg','.png','.jpeg','.gif'),"100","100");
if($handle = opendir($config[0])){
while(false !== ($file = readdir($handle))){
foreach($config[1] as $key => $type) {
if(stristr($file, $type) != FALSE) {
echo "<img src=\"".$config[0]."/".$file."\" width=\"".$config[2]."\" height=\"".$config[3]."\">";
}
}
}
closedir($handle);
}
?>

Keleth
07-31-2012, 03:26 PM
And rather then bothering with opendir and readdir and all that stuff that adds needless code, use glob (http://php.net/manual/en/function.glob.php)


EDIT: I didn't realize this has been a zombied thread, sorry for the post.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum