1

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?

3 Answers 3

3

Your if() structure is broken. Missing a beginning and ending curly brace.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
      }
    }
?>

3 Comments

Here's what the above code returns - "; } } ?> '; foreach($dir as $file){ if(!$dir->isDot() && 0 !== strpos('.', $file->getFilename()) && !$file->isDir()){ $filepath = $path . $file->getFilename(); echo sprintf($img, $filepath, $filepath); } } ?> Seems like something I'm doing in JQuery is writing the PHP as it's written.
you do have php installed and running correct? And youre accessing the file via http:// not file:// correct? Also is there any code being included before this?
You are correct. I didn't even look at the address bar. Once I viewed using localhost, it worked. Thanks for the help.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.