0

I would like to make a MP3 or Audio page and was wondering how I could make an array for files that were upload to the folder from the users then be echoed out on the page as an USABLE MP3 or other audio file, the newer files that were last uploaded would need to be on the top of the page. I have an upload page working that takes the files to a folder but that is it.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

Hello You can use inbuilt php function named scandir() it will return you the array of files when you pass the folder path in the parameter.

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

Example:

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

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

Output:

Array
(
    [0] => .
    [1] => ..
    [2] => bar.php
    [3] => foo.txt
    [4] => somedir
)
Array
(
    [0] => somedir
    [1] => foo.txt
    [2] => bar.php
    [3] => ..
    [4] => .
)
Veerendra
  • 2,562
  • 2
  • 22
  • 39
0

I think it'd be something like...

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

and then You'd need to modify the scandir function. This thread seems to cover that.

scandir() to sort by date modified

The regex can be modified by adding in any other extensions you want separated by a pipe. It is case sensitive currently append an 'i' after the tilde if you dont want that.

Community
  • 1
  • 1
chris85
  • 23,846
  • 7
  • 34
  • 51