0

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.

8
  • do you mean your array value contains comma at the end and you want to remove those? Commented Dec 6, 2018 at 2:30
  • Yeah, but not on all variables. Commented Dec 6, 2018 at 2:33
  • 1
    ls -m? Why not scandir, glob or even loop readdir? Commented Dec 6, 2018 at 2:40
  • I also think glob is very powerfull for this kind of task Commented Dec 6, 2018 at 2:42
  • Because ls -m will always execute in the current directory. I heard that glob and others needed more tweaking. Commented Dec 6, 2018 at 2:42

2 Answers 2

1

The output of ls -m has commas between names on the same line, which are followed by a space, and it also has a comma at the end of each line, which is followed by a newline. You're replacing the comma-space with newline, but you're leaving comma-newline unchanged. You need to replace both.

$cmdsk = str_replace([", ", ",\n"],"\n","$cmd");

I'm not sure why you're parsing ls instead of using PHP's built-in directory listing functions like glob(). You could also use ls -1 instead of ls -m, which puts each filename on its own line.

Sign up to request clarification or add additional context in comments.

Comments

0
$path = trim($path);

if ($path[strlen($path) - 1] == ',') $path = substr($path, 0, -1);

Not the prettiest code, but it removes any space before and after $path, checks if the last character is a comma and if so, removes it.

You could also use str_replace with just the commas without the space, like this:

$cmdsk = str_replace(",", "\n", $cmd);

Or use explode() with the commas directly.

4 Comments

Your solution works, but it creates an empty variable just after the modified variable. Wierd..
Which one of the solutions? You may modify it to fit your needs, you could also do a $path = str_replace(",", "", $path); inside the loop.
I was talking about the second one. Replaced $cmdsk = str_replace(", ","\n","$cmd"); by yours. Do you know where I should add the first one in my code? What should I remove, im confused. Im also pretty noobish in PHP. Started learning a week ago.
Both the first example and the last one I added in my last comments should be placed inside the loop. But note that this is a pretty ugly way of doing this. Using blob or similar would be better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.