View Full Version : read directory
bluewalrus
10-05-2008, 03:09 AM
Anyone know of a php or java script that will read a directory and give all the files in it a name with a number after it. I'm trying to make a simple image gallery for someone that doesnt know html or any coding just how to put images into an ftp.
ok back to the code
in the html file i want it to write something like <a href="$file1.jpg"><img src="$file1.jpg"></a>
$file1.jpg being the first file in the directory and then continue writing those until it gets to the last image in the directory.
PHP:
<?php
define('IMG_DIR', '/image/directory');
function is_image($fn) {
return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
}
$imgs = array_filter(scandir(IMG_DIR), 'is_image');
for ($i = 0; $i < count($imgs); ++$i)
$imgs[$i] = IMG_DIR . $imgs[$i];
?>
<!-- ... -->
<?php foreach ($imgs as $img) { ?>
<a href="$img">
<img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
</a>
<?php } ?>
bluewalrus
10-05-2008, 03:05 PM
This is bringing back an error message each time i try it here http://www.bluewalrus.net/testing.php.
It says "
Parse error: parse error, unexpected ';' in /hsphere/local/home/crazychr/bluewalrus.net/testing.php on line 17
"
Which is in dreamweaver for me foreach ($i = 0; $i < count($imgs); ++$i)
Any idea on that?
Thanks
Master_script_maker
10-05-2008, 03:31 PM
try
<?php
define('IMG_DIR', '/image/directory');
function is_image($fn) {
return in_array(pathinfo($fn, PATHINFO_EXTENSION), array('jpg', 'jpeg', 'png', 'gif'));
}
$imgs = array_filter(scandir(IMG_DIR), 'is_image');
for ($i = 0; $i < count($imgs); ++$i)
$imgs[$i] = IMG_DIR . $imgs[$i];
?>
<!-- ... -->
<?php foreach ($imgs as $img) { ?>
<a href="$img">
<img src="$img" alt="pathinfo($img, PATHINFO_FILENAME);">
</a>
<?php } ?>
Oops, aye. Started off with a foreach then switched to a for :)
Edited to avoid confusion.
bluewalrus
10-05-2008, 07:03 PM
Brings up this now:
Fatal error: Call to undefined function: scandir() in /hsphere/local/home/crazychr/bluewalrus.net/testing.php on line 16
Medyman
10-05-2008, 07:13 PM
http://greg-j.com/phpdl/
I wrote the code for PHP5, but it seems that you are still using PHP4. I strongly suggest you upgrade: PHP4 is no longer supported by the PHP team.
For the meantime, here is the PHP4 version:
<?php
define('IMG_DIR', '/image/directory');
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 } ?>
bluewalrus
10-05-2008, 07:44 PM
hmm i don't know where the php control setting is to up it to 5 yet. So right now i have the 4 up on my page but its not writing the images still for some reason. When i look at the source in a web browser it just has a comments tag (<!-- ... -->).
http://www.bluewalrus.net/testing.php
I only changed the directory in the php code so that it would link correctly.
I'll put in the whole pages html in case there's something I've put in wrong in the validation side as well.
<!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>
Just went into my host and i think i had php only set to 3 so i think i just added 4 and 5 if this is what that looks like?
<IfModule mod_php4.c>
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
AddType application/x-httpd-php .php5
AddType application/x-httpd-php .php4
bluewalrus
10-08-2008, 01:13 AM
For those looking for this there's a closer script in the php board under my name that also went unanswered after the second post but that one got one image to display.
bluewalrus
10-16-2008, 06:13 PM
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);
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.