cosmichero2025
cosmichero2025

Reputation: 1029

How to get a file name from a directory using php and javascript

I'm trying to update an old flash gallery site of mine using php. I'm wondering how I can get a file name randomly from the folder that stores all of the flashs after the original flash has been middle mouse clicked. I'm new to php and I feel as if I have some things mixed up and I have gaps in my knowledge.

HTML

<?php include 'header.php'; ?>

<div id="flash-container">
    <object id="flash-content" data="swfs/sunshine.swf" type="application/x-shockwave-flash"></object>
    </div>

<?php include 'footer.php'; ?>

PHP

<?php
function random_flash($dir = 'swfs')
{
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    return $files[$file];
}
?>

Javascript

$(document).ready(function () {
    $("#flash-content").on('click', function (e) {
        $.ajax({
            type: "GET"
            , url: "flash.php"
            , data: {
                fileName: "$file"
            }
        }).done(function (msg) {
            alert("Data Saved: " + msg);
        });
        if (e.which == 2) {
            e.preventDefault();
            flash - container.innerHTML = '<object id="flashcontent" data="' + $file + '">' + '<param name="movie" type="application/x-shockwave-flash">' + '</object>';
        }
    });
});

Upvotes: 0

Views: 57

Answers (1)

Georges O.
Georges O.

Reputation: 992

I think there are some error on your code.

If you call php, even in a javascript code ... you need to use <?php and ?>.

On your javascript code, I can see $file. Okay, but what is it ? Should be <?php echo $file; ?> no ?

And in your JS code : flash - container.innerHTML What is the object flash ? You can't substract object like this :) (Typo error ?)


So:

  1. On your html web page randered, open the Console Editor and check if tehre is any error.
  2. Open the source code of the page and check if the html/javascript seems good. If you are php error -> check the php code.

You can also and somewhere a <?php echo $file; ?> or <?php echo random_flash(); ?> to check if the value return is a file and the expected value.

Upvotes: 1

Related Questions