Im making an autoindex in PHP. Im trying to sort an array of files in a folder. Here is a part of my code:
<?php
$cmd = shell_exec("ls -m");
$cmdsk = str_replace(", ","\n","$cmd");
$divide = explode("\n", $cmdsk);
array_pop($divide);
foreach( $divide as $path ) {
echo "<th class=\"icon\"><img src=\"/beta/.res/save.png\"></th><th><a href=\"{$path}\">{$path}</a></th></tr>";
}
?>
The output of ls -m looks like this:
file1, file2, file3, etc...
But for some reason, in the array that this code creates, variables like "file1,file2" appears randomly. I sorted this issue out tho- Only thing that bothers me now is that theres random variables like "file3," appearing every so often. It has a comma at the end, so it "corrupts" the file name and the file path (as you might have gessed).
So my question is, how do you remove those random commas appearing at end of variables in the array?
Cheers.
ls -m? Why notscandir,globor even loopreaddir?globis very powerfull for this kind of taskls -mwill always execute in the current directory. I heard that glob and others needed more tweaking.