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.
Asked
Active
Viewed 79 times
0
-
2Have you tried anything yet? – Raptor Dec 08 '14 at 04:26
-
Yes I have , but it would only echo the name out of the file – ChristLovesme97 Dec 08 '14 at 04:27
-
possible duplicate of [Getting the names of all files in a directory with PHP](http://stackoverflow.com/questions/2922954/getting-the-names-of-all-files-in-a-directory-with-php) – Sean Dec 08 '14 at 04:28
2 Answers
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.