0

I'm trying to run a java jar file 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

2
  • Sorry I have now added a more real workld. As you can see path comes from a loop Commented Apr 15, 2010 at 12:41
  • I found the answer myself, Just adding quote around the path resolved the issue! ie, $f = java -jar /OCR/ocr.jar '/Folder/$path' /ocr/output.txt; Commented Apr 15, 2010 at 14:40

5 Answers 5

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0
$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>";

Comments

0

add the same \ in PHP?

Comments

0

Use quotes?

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

Comments

0

Why not escape the space even in the back ticks:

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.