Reputation: 13
I'm trying to use a PHP function to read a server directory for images. The images will then need to be passed back to JQuery using .ajax() for use in a slideshow. I'm using the PHP function hoping to simplify adding/removing images for my user. Basically, all they'd have to do is add or remove images from the directory to modify the slideshow and not edit any code.
I'm struggling to get the JQuery function to return what I'm expecting. It's been a while since I've done PHP so I may be overlooking someone. Here's my PHP code:
<?php
$path = "./";
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
echo "<img src=\"" . $path.$file. " alt=\"" . $file . "\>";
{
}
?>
Here's my readImages JQuery function:
function readImages() {
$.ajax({
type: "POST",
url: "getimages.php",
dataType: "html",
success: function(imagepath){
$(".slideshow").html(imagepath);
}
});
}
What I'm trying to do is get the PHP to return an HTML formatted tag with the file name added. The JQuery then adds the returned tag to the HTML code.
So far... all it's returning is "; { } ?>
Any ideas as to what I'm doing wrong?
Upvotes: 1
Views: 1250
Reputation: 60413
The main issue is the borken if structure Rob Olmos pointed out. But i would also switch to directory iterator instead of readfile
if youre using php5.
<?php
$path = "./";
$dir = new DirectoryIterator(realpath($path));
$img = '<img src="%s" alt="%s" />';
foreach($dir as $file){
if(!$dir->isDot() && 0 !== strpos('.', $file->getFilename()) && !$file->isDir()){
$filepath = $path . $file->getFilename();
echo sprintf($img, $filepath, $filepath);
}
}
?>
Upvotes: 0
Reputation: 21175
Since you say it's returning this...
"; { } ?>
My first guess would be you have a syntax error somewhere in closing your PHP up. Missing quote maybe?
The rest of this looks correct.
Upvotes: 0
Reputation: 2432
Your if() structure is broken. Missing a beginning and ending curly brace.
Upvotes: 3