开发者

PHP Path issue running backticks/exec()

开发者 https://www.devze.com 2022-12-27 13:22 出处:网络
I\'m trying to run a java jar fi开发者_开发技巧le from the command line and within the the execution it gives a path. Withing this path their are spaces and this is causing the issue.

I'm trying to run a java jar fi开发者_开发技巧le from the command line and within the the execution it gives a path. Withing this path their are spaces and this is causing the issue.

ie

foreach($paths as $path):
$f = `java -jar /OCR/ocr.jar /Folder/$path /ocr/output.txt`;
echo "<pre>$output</pre>";
endforeach;

If you can see the space in between the Sub Folder name causes the issue.

By command line it would be (which works)

java -jar /OCR/ocr.jar /Folder/Sub\ Folder/filetoocr.pdf /ocr/output.txt

any suggestions how I can resolve this ??

Hope you can advise


Use escapeshellarg():

escapeshellarg() adds single quotes around a string and quotes/escapes any existing single quotes allowing you to pass a string directly to a shell function and having it be treated as a single safe argument.

and maybe escapeshellcmd()

$cmd = sprintf(
  'java -jar %s %s %s',
  escapeshellarg('/OCR/ocr.jar'),
  escapeshellarg('/Folder/Sub Folder/filetoocr.pdf'),
  escapeshellarg('/ocr/output.txt')
);
echo 'Debug: cmd=', $cmd;


$f = "java -jar /OCR/ocr.jar /Folder/Sub\\ Folder/filetoocr.pdf /ocr/output.txt";
echo "<pre>$output</pre>";

or

$f = 'java -jar /OCR/ocr.jar "/Folder/Sub Folder/filetoocr.pdf" /ocr/output.txt';
echo "<pre>$output</pre>";


add the same \ in PHP?


Use quotes?

$f = `java -jar /OCR/ocr.jar '/Folder/Sub Folder/filetoocr.pdf' /ocr/output.txt`;


Why not escape the space even in the back ticks:

$f = `java -jar /OCR/ocr.jar /Folder/Sub\ Folder/filetoocr.pdf /ocr/output.txt`;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号