PDA

View Full Version : write images from dir



bluewalrus
10-07-2008, 01:04 AM
I have the code below which i got from the looking for a code or script board but no one there is answering now. The code is suppose to read my directory called images and write <a href="image# in directory"><img scr="image# in directory"></a>
Then keep writing till it gets to the last image. This is the code I got but it is only writing 1 comment tag(<!-- ... -->) . i have about 50 images in the images directory and 3 other directories with in that directory would that make an error?
http://www.bluewalrus.net/testing.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
define('IMG_DIR', 'images/');

function is_image($fn) {
return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
}
$imgs = array();
$dir = opendir(IMG_DIR);
while ($ff = readdir($dir))
if (is_image($f))
$imgs[] = IMG_DIR . '/' . $f;
closedir($dir);
?>
<!-- ... -->
<?php foreach ($imgs as $img) { ?>
<a href="$img">
<img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
</a>
<?php } ?>
</body>
</html>

Any ideas or suggestions i appreciate thanks.

rangana
10-07-2008, 02:44 AM
You had typos and errors:



while ($ff = readdir($dir))
if (is_image($f))
$imgs[] = IMG_DIR . '/' . $f;

$f does'nt exist, you mean $ff instead.


You define IMG_DIR with 'images/' string, so this part makes it erroneous:


$imgs[] = IMG_DIR . '/' . $f;

...it will become images//


You need to place the variables inside the PHP code:


<a href="$img">
<img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">



The rectified version is:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
define('IMG_DIR', 'images');

function is_image($fn) {
return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
}
$imgs = array();
$dir = opendir(IMG_DIR);
while ($ff = readdir($dir))
if (is_image($ff))
$imgs[] = IMG_DIR . '/' . $ff;
closedir($dir);
?>
<?php foreach ($imgs as $img)
{
echo '<a href="'.$img.'">
<img src="'.$img.'" alt="'.pathinfo($img, PATHINFO_FILENAME).'">
</a>';
}
?>
</body>
</html>


Hope that helps.

bluewalrus
10-07-2008, 03:09 AM
Oh oh. Thanks that's the closest i've been thus far but thats only bringing up one image from the directory and this error message
Warning: pathinfo() expects parameter 2 to be long, string given in /hsphere/local/home/crazychr/bluewalrus.net/testing.php on line 25.
Any Ideas?

bluewalrus
10-16-2008, 06:16 PM
For anyone looking to write all the images in a directory with a link to the file...

The script for reading images directory and writing the image with a link to the actual file...


<?php
//getting all files of desired extension from the dir using explode
$dirname = "images/";
$dir = opendir($dirname);
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
$fileChunks = explode(".", $file);
if($fileChunks[1] == 'jpg' || 'jpeg' || 'gif' || 'png') //File types to be read
{
echo '<a href="images/'.$file.'" ><img src="images/' . $file . '" border="0" /></a></br>';
}
}
}
closedir($dir);
?>