2014-12-08 20 views
0

我想製作一個MP3或音頻頁面,並想知道如何爲從用戶上傳到文件夾的文件創建一個陣列,然後作爲可用的MP3在頁面上回顯或其他音頻文件,上次上傳的較新文件需要位於頁面的頂部。我有一個上傳頁面工作,將文件到一個文件夾,但就是這樣。從一個文件夾中迴應文件

+2

你嘗試過什麼嗎? – Raptor 2014-12-08 04:26:15

+0

是的我有,但它只會回顯文件名稱 – ChristLovesme97 2014-12-08 04:27:37

+0

可能的重複[使用PHP獲取目錄中的所有文件的名稱](http://stackoverflow.com/questions/2922954/getting-the-名稱 - 的 - 所有文件-IN-A-目錄與-PHP) – Sean 2014-12-08 04:28:35

回答

0

你好你可以使用名爲scandir()的內置php函數,當你傳遞參數中的文件夾路徑時,它會返回你的文件數組。

http://php.net/manual/en/function.scandir.php

例子:

<?php 
$dir = '/tmp'; 
$files1 = scandir($dir); 
$files2 = scandir($dir, 1); 

print_r($files1); 
print_r($files2); 
?> 

輸出:

Array 
(
    [0] => . 
    [1] => .. 
    [2] => bar.php 
    [3] => foo.txt 
    [4] => somedir 
) 
Array 
(
    [0] => somedir 
    [1] => foo.txt 
    [2] => bar.php 
    [3] => .. 
    [4] => . 
) 
0

我覺得這是像...

$dir = '/mp3s'; 
$files = scandir($dir); 
foreach($files as $file) { 
    if(preg_match('~\.(mp3|EXTEnSION)$~'. $file)) { 
     echo '<a href="' . $dir . '/' . $file . '">' . $file , '</a>'; 
    } 
} 

,然後你會需要修改scandir f結。這個主題似乎涵蓋了這一點。

scandir() to sort by date modified

正則表達式可以通過在你想要由豎線任何其他擴展添加修改。如果你不想要的話,目前在波浪號後面加上'i'。

相關問題