Search dir using wildcard string, return filename, and loop
I'm having some problem开发者_C百科s, and looking for some help. I'm trying to create a photo gallery in javascript, that will be able to 'update' it's self automatically. I need to be able to search a directory, and grab a file with a specific prefix.
I need to output the followng HTML code:
<li><a href="images/resize_FILENAME.ext"><img src="images/thumb_FILENAME.ext"></a></li>
The 'resize_' and 'thumb_' use a timestamp to identify, so they have the same ending, just a different prefix.
So, for example, if I search the directory for an image with a prefix of 'resize_', I need to insert that into the a tag, and then remove the '_resize' prefix, and add the '_thumb' prefix for the img tag. I then need to be able to do that for each image in the directory.
Any help would be greatly appreciated.
Oh, I should add:
I'm assuming php would be easiest for this, but if an alternative exists that would work too. I'm also using jQuery if javascript would be better.
After some reading about the glob()
function, I was able to figure it out pretty easily.
Here's my code:
<?php
foreach (glob("upload_pic/thumbnail_*.*") as $thumb) {
$resize = preg_replace("/thumbnail_/", "resize_", $thumb);
echo "<li><a href='$resize'><img src='$thumb'></a></li>";
}
?>
So, basically, the glob()
function searches my upload_pic
directory for any file with the thumbnail_
prefix. I then create run the preg_replace()
function to replace the $thumb
variable's thumbnail_
with resize_
, and make that the $resize
variable. Then just echo
the proper HTML code.
To search a directory using wildcard string, glob()
function can be used.
But how to learn AJAX and use glob()
results from the PHP script on the client side, is probably another question.
精彩评论