簡體   English   中英

在服務器文件夾中獲取文件名的工作解決方案

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

我在 Stackoverflow 上使用了多種解決方案,但我無法使它們中的任何一個正常工作(不接受 Nodejs)。

我有一個充滿 mp3 文件的路徑,我想返回文件名。

我嘗試的最后一件事是:

在我服務器的getFiles文件夾中,我有一個名為files.php的 PHP 文件和一個名為scripts.jsindex.html的 JS 文件

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:

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

mp3文件所在目錄為: mysite/folderOf/audio/

上面代碼的結果是:

在此處輸入圖片說明

我需要返回一個包含mysite/folderOf/audio/目錄文件名的數組。

試試這個,如果它有效

    <?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