<!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
$dirname = "/Users/jakeboyles/Desktop/CashFlowStuff";
$dp = opendir($dirname);
chdir($dirname);
while ($currentFile !== false) {
$currentFile = readdir($dp);
$theFiles[] = $currentFile;
}
$imageFiles = preg_grep("/jpg$|gif$|png$/", $theFiles);
$output="";
foreach ($imagesFiles as $currentFile) {
$output .= <<<HERE
<a href = $currentFile>
<img src= "$currentFile"
height = 50
width=50>
</a>
HERE;
}
$fp = fopen("imageIndex.html","w");
fputs ($fp,$output);
fclose($fp);
//print "hello";
?>
</body>
</html>
Page 1 of 1
Help with PHP
#1
Posted 14 January 2012 - 03:38 AM
I am learning PHP at the moment and have this script. It takes the images from a dir and adds them to a web site. Why isnt this working? I cant figure it out....
#2
Posted 14 January 2012 - 12:01 PM
I have a better way of doing this..
That line selects all files, so you may want to ensure that all files withhin that directory are images. If not, you can specify the type of images you want to output.
Instead of the above:
That will select all .png images
<?php
$imagedir = "path/to/dir/"; //replace with the path to the images directory
$images = glob($imagedir."*.*"); // selects all images
foreach($images as $image)
{
echo "<a href='".$image."'>".$image."</a>"
}
$images = glob($imagedir."*.*"); // selects all images
That line selects all files, so you may want to ensure that all files withhin that directory are images. If not, you can specify the type of images you want to output.
Instead of the above:
$images = glob($imagedir."*.png"); // selects all png images
That will select all .png images
This post has been edited by Samus: 14 January 2012 - 12:04 PM
#3
Posted 14 January 2012 - 03:18 PM
Samus, on 14 January 2012 - 12:01 PM, said:
I have a better way of doing this..
glob, really? How on earth is that better? If anything, the OPs code using opendir, that doesn't work, is better than glob.
@OP - have a read about SPL and the DirectoryIterator class. See http://php.net/manua...oryiterator.php
#4
Posted 14 January 2012 - 03:30 PM
rallport, on 14 January 2012 - 03:18 PM, said:
glob, really? How on earth is that better? If anything, the OPs code using opendir, that doesn't work, is better than glob.
@OP - have a read about SPL and the DirectoryIterator class. See http://php.net/manua...oryiterator.php
@OP - have a read about SPL and the DirectoryIterator class. See http://php.net/manua...oryiterator.php
why not?
Share this topic:
Page 1 of 1
Help
















