Skip to content

Passing a command with spaces on Windows doesn't work #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DomiQ opened this issue Aug 20, 2014 · 14 comments
Closed

Passing a command with spaces on Windows doesn't work #1

DomiQ opened this issue Aug 20, 2014 · 14 comments

Comments

@DomiQ
Copy link

DomiQ commented Aug 20, 2014

Sorry for moving the thread around, I got really confused with all the code.
When I try passing this as an exaple:

 '..\..\..\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe http://localhost/Ponudomat/printRdy.php print.pdf'

The program returns this:

'..\..\..\Program' is not recognized as an internal or external command, operable program or batch file.

It separates the DIR into different commands because of the spaces, to fix it you need to pass the DIR within the "" and that's where the problem kicks in.

When I try:

 '"..\..\..\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" http://localhost/Ponudomat/printRdy.php print.pdf'

I just keep getting the same error as the one above.
When I try adding "/ instead of " to try and force php into making a quote within a string.
This is the confusing part, when I put:

 '"/..\..\..\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"/ http://localhost/Ponudomat/printRdy.php print.pdf'

It actually runs the command and partially works. So now I got the .exe running but it gives me an error as it cannot read the parameters it needs because of the "/ so I get:

Loading pages (1/6) [> ] 0% [===> ] 5% [======> ] 10% Error: Failed loading page file:///C:/ (sometimes it will work just to ignore this error with --load-error-handling ignore) [========> ] 14% [=========> ] 15% [==========> ] 17% [============> ] 20% [=================================> ] 55% Exit with code 1 due to network error: ContentOperationNotPermittedError

Because the command is as follows getCommand:

/..\..\..\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"/ http://localhost/Ponudomat/printRdy.php print.pdf
@mikehaertl
Copy link
Owner

Thanks. TBH I wont find much time currently to look deeper into this issue - which is moreover a bit hard for me, since I don't use any Windows.

If you happen to find a solution, I'd be happy to include it. The code for the class itself is not very complicated. Most if it is used to manage console arguments. The main magic with escaping happens in setCommand(). You may also want to play around with escapeArgs and escapeCommand properties.

Thanks for helping in improving this class for Windows users!

@mikehaertl
Copy link
Owner

One more thing: You seem to have messed up the order when you try to escape a character. First, inside ' you don't to escape any ". If you need a ' inside of single quotes, you have to write it as \' not as '\. Just saying, maybe i was misreading.

@DomiQ
Copy link
Author

DomiQ commented Aug 20, 2014

I've done some digging around and it turns out that windows command shell has a preset shortcut for "/Program Files" witch is "/Progra~1/". this is only works for the directory in question and is not applicable to the rest of the file system as far as I know
This works when I pass it, mby we should add a notice to documentation but it's not really something worth coding for in my opinion.

@DomiQ DomiQ closed this as completed Aug 20, 2014
@DomiQ
Copy link
Author

DomiQ commented Aug 20, 2014

This does mean that anybody accessing files with spaces in windows might have trouble but I'll look into that some more and try to find a way to use thees quotes.
Because there's really no other way to access dose.

@mikehaertl
Copy link
Owner

I'm pretty sure that there's a way to make it work with spaces. Let's keep this issue open for reference.

@mikehaertl
Copy link
Owner

Here's a comment from the proc_open() PHP manual page that seems to address and solve the issue:

If you are working on Windows and try to proc_open an executable that contains spaces in its path, you will get into trouble.

But there's a workaround which works quite well. I have found it here: http://stackoverflow.com/a/4410389/1119601

For example, if you want to execute "C:\Program Files\nodejs\node.exe", you will get the error that the command could not be found.
Try this:

<?php
$cmd = 'C:\\Program Files\\nodejs\\node.exe';
if (strtolower(substr(PHP_OS,0,3)) === 'win') {
    $cmd = sprintf('cd %s && %s', escapeshellarg(dirname($cmd)), basename($cmd));
}
?>

@mikehaertl
Copy link
Owner

@DomiQ In case you're still there: If I add the above fix, would you be available for testing? Because I don't use Windows.

@DomiQ
Copy link
Author

DomiQ commented May 23, 2016

I can test it sure, but I've got a bunch of projects to finish for college.
So I'll test it but probably not in the coming month.

On Fri, 20 May 2016 at 13:05 Michael Härtl [email protected] wrote:

@DomiQ https://github.com/DomiQ In case you're still there: If I add
the above fix, would you be available for testing? Because I don't use
Windows.


You are receiving this because you were mentioned.

Reply to this email directly or view it on GitHub
#1 (comment)

@mikehaertl
Copy link
Owner

That's great. I've pushed the fix to master. Maybe you find the time for a quick test. I could also create a release, if that's easier for you.

@DomiQ
Copy link
Author

DomiQ commented May 23, 2016

Ok, great, master is fine. I'll test it when I find some free time.

On Mon, 23 May 2016 at 18:29 Michael Härtl [email protected] wrote:

That's great. I've pushed the fix to master. Maybe you find the time for a
quick test. I could also create a release, if that's easier for you.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1 (comment)

@mikehaertl
Copy link
Owner

@DomiQ Ping ;). Did you maybe find a minute to give this a quick try? Should really not be hard to test. I just don't have Windows here so can't test it myselft.

@DomiQ
Copy link
Author

DomiQ commented Jul 6, 2016

@mikehaertl I'm just now rerunning home from a trip so I can give it a run tonight or tomorrow morning and report back. :)

@DomiQ
Copy link
Author

DomiQ commented Jul 7, 2016

I've tested the commit on a project I worked on back when the issue started and I can confirm that it works. I've also just tried pushing some bigger commands trough it and they all worked. Sorry it took me a while to test.

@mikehaertl
Copy link
Owner

Great news! Thanks! So I'll close this and prepare release 1.2.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants