PHP wrapper incorrectly quotes file path on Windows
We are using Prince on Windows Server, and it is installed under C:\Program Files (x86). Our PHP code to instantiate Prince looks like this:
$prince = new Prince('C:\Program Files (x86)\Prince\Engine\bin\prince.exe');
However, this doesn't work because the addDoubleQuotes() method in the PHP wrapper puts quotes around spaces in the path, which the Windows command line doesn't appear to accept.
// this is what the unmodified addDoubleQuotes() method returns //
C:\Program" "Files" "(x86)\Prince\Engine\bin\prince.exe
We were able to fix this issue by modifying the Prince PHP wrapper file and placing the following code at the top of the addDoubleQuotes() method:
// simply returns the path with double quotes at the beginning and end //
return '"' . $str . '"';
It would be great if the official PHP wrapper could be updated to fix this bug, so that we don't have to maintain a modified version, and so others won't run into this issue.
Thanks, we have updated the Prince PHP wrapper to quote parentheses as well as spaces, and a few other special characters like & and ^ which were also causing problems. It should work fine now.
We tried quoting the whole executable path, but proc_open() consistently fails when the command-line begins with a quote, and we can't find any way around this, which is why we settled on applying the quotes only around the special characters. Perhaps you are running a different version of PHP to us, or there is some other environment difference which is allowing the command-line to start with a quote. Either way, this new wrapper should hopefully work properly in both environments.
Mike,
Thank you very much for the quick update. Release 11 of the PHP wrapper works perfectly for us.
Hi, I'm running into this issue right now, using the wrapper class prince-php5-r14.zip
Indeed, whenever I try to convert a file, I can see that line 583 of the Prince class has the first part of $pathAndArgs equal to:
'c:\Program" "Files" ("x86")"\Prince\Engine\bin\prince.exe'
Just wondering if I downloaded the wrong wrapper or???
So it is failing to run the executable in this case? That's strange, the quoting was there for a reason, and fixed the issue with spaces we were encountering earlier. What if you try this
alpha version of the PHP wrapper, which adds the bypass_shell flag to proc_open?
Hi mikeday, indeed it's failing with a PHP warning, " Warning: proc_open(): CreateProcess failed, error code - 2" as the quotes are messing up the path to the executable. The alpha made no difference. I can't see why theodorejb's seemed to work unless there was some sort of code regression. Either way his earlier posted solution of adjust addDoubleQuotes to just return that string works fine for our use.
Yes it is still broken. I installed prince today to evaluate for our company. So prince php lib wraps any spaces in " " so [c:\program files\blaah] gets set to [c:\program" "files\blaah] instead of ["c:\program files\blaah"].
What i don't understand i why you don't just double the quote the whole path regardless of if it has space or special chars. So in your php lib just do:-
public function __construct($exePath)
{
$this->exePath = '"'.ltrim($exePath).'"';
May I ask what version of PHP you are using, and on which operating system?
Last year we added the space quoting specifically because quoting the entire string was failing on Windows (XP and Vista) and quoting the spaces and other special characters appeared to be the only interoperable way of fixing it. As I mentioned at the time:
We tried quoting the whole executable path, but proc_open() consistently fails when the command-line begins with a quote, and we can't find any way around this, which is why we settled on applying the quotes only around the special characters. Perhaps you are running a different version of PHP to us, or there is some other environment difference which is allowing the command-line to start with a quote. Either way, this new wrapper should hopefully work properly in both environments.
If it's not working, then we are back to square one. We can always just quote the entire string, and see if that breaks for anyone, or leave both options available to try. Perhaps the bypass_shell flag will improve matters, I'm not sure yet.
We're finding this an issue on Windows 7.
It's solvable a few ways, ie: installing to a path without a space, or just replacing the addDoubleQuotes() method, but I did want to let you know it's still an issue. PHP version 5.5.x