简体   繁体   English

如何使用php和javascript从目录获取文件名

[英]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. 我正在尝试使用php更新我的旧Flash Gallery网站。 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. 我是php的新手,感觉好像有些事情混在一起了,我的知识也空白。

HTML 的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

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

Javascript Java脚本

$(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>';
        }
    });
});

I think there are some error on your code. 我认为您的代码有一些错误。

If you call php, even in a javascript code ... you need to use <?php and ?> . 如果您调用php,即使使用javascript代码,也需要使用<?php?>

On your javascript code, I can see $file . 在您的JavaScript代码上,我可以看到$file Okay, but what is it ? 好吧,那是什么? Should be <?php echo $file; ?> 应该是<?php echo $file; ?> <?php echo $file; ?> no ? <?php echo $file; ?>不?

And in your JS code : flash - container.innerHTML What is the object flash ? 在您的JS代码中: flash - container.innerHTML什么是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. 在已标记HTML网页上,打开控制台编辑器,然后检查tehre是否有任何错误。
  2. Open the source code of the page and check if the html/javascript seems good. 打开页面的源代码,然后检查html / javascript是否良好。 If you are php error -> check the php code. 如果您是php错误->检查php代码。

You can also and somewhere a <?php echo $file; ?> 您还可以在<?php echo $file; ?>某处<?php echo $file; ?> <?php echo $file; ?> or <?php echo random_flash(); ?> <?php echo $file; ?><?php echo random_flash(); ?> <?php echo random_flash(); ?> to check if the value return is a file and the expected value. <?php echo random_flash(); ?>检查返回的值是否是文件以及期望值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM