2

No matter how hard I try, mkvextract doesn't work properly. I'm aware that there is a problem with the file path, but I tried hundreds of times, but I still could not succeed. How can I run this correctly?

shell_exec("mkvextract tracks /home/movies/R-12/X-1 ÇĞŞZ.mkv");

or

$filename = "/home/movies/R-12/X-1 ÇĞŞZ.mkv"
echo shell_exec("mkvextract tracks \"$filename\"");

I am aware that you cannot access the file path due to special characters

sweetngx
  • 67
  • 4
  • 1
    Does this answer your question? [How do I to properly handle spaces in PHP Shell\_exec?](https://stackoverflow.com/questions/378490/how-do-i-to-properly-handle-spaces-in-php-shell-exec) – CBroe Jul 27 '22 at 06:10

2 Answers2

1

There may be several issues:

  • A file read permision issue: the file exists, but PHP (and the mkvextract it runs) don't have the permission to open it. In the rest of my answer I assume this is not happening, because you haven't added any error message containg the word permission or access to your question.
  • A shell argument escaping issue: correcly passing a command argument containing whitespace and/or shell metacharacters (e.g. ", \, $). I address this with escapeshellarg below.
  • A filename encoding issue: correctly specifying non-ASCII characters in filenames. I address this with mb_convert_encoding below.

For testing purposes, make a copy of the input file to /home/movies/t.mkv, and then try echo shell_exec("mkvextract tracks /home/movies/t.mkv").

If that works, then rename the copy to /home/movies/t t.mkv, and then try echo shell_exec("mkvextract tracks " . escapeshellarg("/home/movies/t t.mkv")). Without the escapeshellarg call, it wouldn't work, because the filename contains a space.

If that works, then the problem is with non-ASCII characters in the filename. To investigate it further, examine the output of var_dump(scandir("/home/movies/R-12")), and see how the letters with accents appear there. Pass it the same way to shell_exec. Don't forget about escapeshellarg.

If that works, use encoding conversion (with mb_convert_encoding) for the remaining filenames. You may want to ask a separate question about that, specifying the output of var_dump(scandir("/home/movies/R-12")) and var_dump("X-1 ÇĞŞZ.mkv") in your question.

pts
  • 80,836
  • 20
  • 110
  • 183
0
$filename = "/home/movies/R-12/X-1 ÇĞŞZ.mkv"
echo shell_exec("sudo mkvextract tracks \"$filename\"");

I guess the whole problem was not adding sudo per :)

sweetngx
  • 67
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Gary Houbre Jul 28 '22 at 10:07