简体   繁体   English

在服务器文件夹中获取文件名的工作解决方案

[英]Working solution to get file names inside server folder

I've used multiple solutions here on Stackoverflow but I can't make any of them work properly (Nodejs is not accepted).我在 Stackoverflow 上使用了多种解决方案,但我无法使它们中的任何一个正常工作(不接受 Nodejs)。

I have a path filled with mp3 files and I want to return the file names.我有一个充满 mp3 文件的路径,我想返回文件名。

The lasted thing I tried was this:我尝试的最后一件事是:

in the getFiles folder of my server, I have a PHP file named files.php and a JS file named scripts.js and index.html在我服务器的getFiles文件夹中,我有一个名为files.php的 PHP 文件和一个名为scripts.jsindex.html的 JS 文件

PHP code: PHP代码:

<?php
     $dir = 'mysite/folderOf/audio/';
    // Store the scandir results in a variable
    $files = scandir($dir);
    // Encode the array in JSON and echo it
    echo json_encode($files);
?>

Javascript: Javascript:

$.get( "files.php", function( data ) {
          console.log(data);
});

The directory where the mp3 files exist is: mysite/folderOf/audio/ mp3文件所在目录为: mysite/folderOf/audio/

The result of the code above is:上面代码的结果是:

在此处输入图片说明

I need to return an array with filenames of mysite/folderOf/audio/ directory.我需要返回一个包含mysite/folderOf/audio/目录文件名的数组。

try this if it works试试这个,如果它有效

    <?php
        // open this directory 
        $Directory = opendir('mysite/folderOf/audio');
        // get each entry
        while($entryName = readdir($Directory)) {
            $fileArray[] = $entryName;
        }
        // close directory
        closedir($Directory);
        //  count elements in array
        sort($fileArray);
        $indexCount = count($fileArray);
        // loop through the array of files and print them all in a list
        for($index=0; $index < $indexCount; $index++) {
            $extension = substr($fileArray[$index], -3);
            if ($extension == 'mp3'){
                echo '<a class="iconMP3" href="mysite/folderOf/audio/' . $fileArray[$index] . '"/>' . $fileArray[$index] . '</a>';
            }   
        }
   ?>

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

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