Executing an application whose directory path contains blank spaces 
July 17th, 2005, 11:50 AM
| | | |
Hello,
I can't find a way to execute a Windows application, whose directory path
contains blank spaces, from a PHP script. I also wonder if the problem
happens under Linux and other OS.
Working dir : "C:\test copy"
"copy.php" PHP script :
<?php
system ('"C:\test copy\mycopy" /B "C:\test copy\folder1\file1" "C:\test
copy\folder2\file2"');
[color=blue]
>[/color]
mycopy.bat batch :
copy %1 %2 %3
Executign this script I get the following error message :
« C:\>php "test copy\copy.php
'C:\test' is not recognized as an internal or external command,
operable program or batch file. »
If I replace « test copy » by « test » or « test_copy », it works.
The problem doesn't happen if the command parameters are not double quoted :
<?php
system ('"C:\test copy\mycopy" /B');
[color=blue]
>[/color]
Note that you can just copy & paste these commands and see how they work in
a DOS command window.
How should I transform these blank spaces characters ? For example in a URL
you replace blank spaces by « %20 »...
The general idea is to execute an application located in the « "C:\Program
Files" directory. On the <http://www.php.net/manual/en/function.system.php>
page of the PHP manual someone proposed a solution but its command line
doesn't involve double quoted parameters.
--
Jean-Marc. | 
July 17th, 2005, 11:50 AM
| | | | re: Executing an application whose directory path contains blank spaces
*** Jean-Marc Molina wrote/escribió (Thu, 20 Jan 2005 09:58:30 +0100):[color=blue]
> mycopy.bat batch :
> copy %1 %2 %3[/color]
You're missing quotes in the batch file?
copy "%1" "%2" "%3"
--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
-- | 
July 17th, 2005, 11:50 AM
| | | | re: Executing an application whose directory path contains blank spaces
Jean-Marc
I have found two ways around this problem:
1. Accept that paths with spaces in are going to get treated as two
arguments and work around it - change line in php script to:
system ('"C:\test copy\mycopy" /B C:\test copy\folder1\file1 C:\test
copy\folder2\file2');
command %1 %2 %3
%4 %5
and then alter your batch file like this:
copy %1 "%2 %3" "%4 %5"
Note spaces between %2 & %3, and between %4 & %5
2. Create your batch file on the fly in the php script - I think this is
neater as you could pass source and destination filepaths in as variables:
<?
$handle=fopen("C:/test copy/mycopy.bat", "w");
$command = "copy /B \"C:\\test copy\\folder1\\file1\" \"C:\\test
copy\\folder1\\file2\"";
//echo "$command<br />\n"; //used for testing
fwrite($handle, $command);
system ("\"C:/test copy/mycopy.bat\""); //note I have escaped the " and used
forward slashes in the path
fclose($handle);
?>
Take your pick!
--
Paul Barfoot
"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41ef72b6$0$9969$636a15ce@news.free.fr...[color=blue]
> Hello,
>
> I can't find a way to execute a Windows application, whose directory path
> contains blank spaces, from a PHP script. I also wonder if the problem
> happens under Linux and other OS.
>
> Working dir : "C:\test copy"
>
> "copy.php" PHP script :
> <?php
>
> system ('"C:\test copy\mycopy" /B "C:\test copy\folder1\file1" "C:\test
> copy\folder2\file2"');
>[color=green]
>>[/color]
>
> mycopy.bat batch :
> copy %1 %2 %3
>
> Executign this script I get the following error message :
> « C:\>php "test copy\copy.php
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »
>
> If I replace « test copy » by « test » or « test_copy », it works.
>
> The problem doesn't happen if the command parameters are not double quoted
> :
>
> <?php
>
> system ('"C:\test copy\mycopy" /B');
>[color=green]
>>[/color]
>
> Note that you can just copy & paste these commands and see how they work
> in
> a DOS command window.
>
> How should I transform these blank spaces characters ? For example in a
> URL
> you replace blank spaces by « %20 »...
>
> The general idea is to execute an application located in the « "C:\Program
> Files" directory. On the
> <http://www.php.net/manual/en/function.system.php>
> page of the PHP manual someone proposed a solution but its command line
> doesn't involve double quoted parameters.
>
> --
> Jean-Marc.
>[/color] | 
July 17th, 2005, 11:50 AM
| | | | re: Executing an application whose directory path contains blank spaces
Alvaro G Vicario a écrit/wrote :[color=blue]
> You're missing quotes in the batch file?
>
> copy "%1" "%2" "%3"[/color]
No, my parameters are already quoted, I think it even double-double-quotes
them :).
Command line :
« C:\>php "C:\test copy\copy.php"
'C:\test' is not recognized as an internal or external command,
operable program or batch file. »
--
Jean-Marc. | 
July 17th, 2005, 11:50 AM
| | | | re: Executing an application whose directory path contains blank spaces
Paul Barfoot a écrit/wrote :[color=blue]
> $command = "copy /B \"C:\\test copy\\folder1\\file1\" \"C:\\test
> copy\\folder1\\file2\"";[/color]
Thanks but I supposed « copy » was located in a path containing blank
spaces, like "C:\Program Files" for example, that's why I used a "C:\test
copy" test folder, this was just an example.
--
Jean-Marc. | 
July 17th, 2005, 11:51 AM
| | | | re: Executing an application whose directory path contains blank spaces
"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41eff055$0$9975$636a15ce@news.free.fr...[color=blue]
> Alvaro G Vicario a écrit/wrote :[color=green]
> > You're missing quotes in the batch file?
> >
> > copy "%1" "%2" "%3"[/color]
>
> No, my parameters are already quoted, I think it even double-double-quotes
> them :).
>
> Command line :
>
> « C:\>php "C:\test copy\copy.php"
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »
>
> --
> Jean-Marc.[/color]
They have to be quoted again in the batch file. Otherwise cmd isn't going to
parse the line correctly. | 
July 17th, 2005, 11:51 AM
| | | | re: Executing an application whose directory path contains blank spaces
Chung Leong a écrit/wrote :[color=blue]
> They have to be quoted again in the batch file. Otherwise cmd isn't
> going to parse the line correctly.[/color]
Did you try it ? Does it work ? What's your output ? What's your OS ?
I tried it. It doesn't work. I get below output and my OS is Windows 2000
Pro.
Try to execute the following "copy.php" PHP script :
<?php
system ('"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
copy\folder1\file1" "C:\test copy\folder2\file2"');
?>
mycopy.bat batch file :
copy "%1" "%2" "%3"
Output :
« >php "C:\test copy\copy.php"
'C:\test' is not recognized as an internal or external command,
operable program or batch file. »
--
Jean-Marc. | 
July 17th, 2005, 11:51 AM
| | | | re: Executing an application whose directory path contains blank spaces
Jean-Marc
Try executing the command you want directly in a Win2000 command window.
Where do you need to put quotes to get that to work? Then put quotes in
either the batch file or the PHP script to mirror this.
That was how I came to the solution I offered in my earlier post. If I
quoted the source and destination filenames in PHP then the system command
didn't work, but if I didn't put quotes around them the parameters were
treated as 5 variables instead of 3!
Regards
--
Paul Barfoot
"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41f0af46$0$28961$8fcfb975@news.wanadoo.fr...[color=blue]
> Chung Leong a écrit/wrote :[color=green]
>> They have to be quoted again in the batch file. Otherwise cmd isn't
>> going to parse the line correctly.[/color]
>
> Did you try it ? Does it work ? What's your output ? What's your OS ?
>
> I tried it. It doesn't work. I get below output and my OS is Windows 2000
> Pro.
>
> Try to execute the following "copy.php" PHP script :
> <?php
>
> system ('"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
> copy\folder1\file1" "C:\test copy\folder2\file2"');
>
> ?>
>
> mycopy.bat batch file :
> copy "%1" "%2" "%3"
>
> Output :
> « >php "C:\test copy\copy.php"
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »
>
> --
> Jean-Marc.
>[/color] | 
July 17th, 2005, 11:52 AM
| | | | re: Executing an application whose directory path contains blank spaces
"Jean-Marc Molina" <jmmolina@PASDEPOURRIEL-free.fr> wrote in message
news:41f0af46$0$28961$8fcfb975@news.wanadoo.fr...[color=blue]
> Chung Leong a écrit/wrote :[color=green]
> > They have to be quoted again in the batch file. Otherwise cmd isn't
> > going to parse the line correctly.[/color]
>
> Did you try it ? Does it work ? What's your output ? What's your OS ?
>
> I tried it. It doesn't work. I get below output and my OS is Windows 2000
> Pro.
>
> Try to execute the following "copy.php" PHP script :
> <?php
>
> system ('"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
> copy\folder1\file1" "C:\test copy\folder2\file2"');
>
> ?>
>
> mycopy.bat batch file :
> copy "%1" "%2" "%3"
>
> Output :
> « >php "C:\test copy\copy.php"
> 'C:\test' is not recognized as an internal or external command,
> operable program or batch file. »[/color]
My apology. I was wrong about the quotes. Didn't realize that cmd is smart
enough to put quotes around a parameter when it expands %1, %2.
The problem, it appears, is caused by a broken implementation of system() in
the C library. To get around, use cmd.exe to parse the command.
Example:
test.bat:
echo %1
echo %2
test.php:
echo `cmd /C ""C:\\test copy\\test.bat" "C:\\Program files" "C:\\Documents
and settings""`
The first and last quotation marks enclose the command, while the others
enclose the parameters. Need to escape slashes here because I'm using
backtick.
Hope that's more useful advise. | 
July 17th, 2005, 11:52 AM
| | | | re: Executing an application whose directory path contains blank spaces
Chung Leong a écrit/wrote :[color=blue]
> The problem, it appears, is caused by a broken implementation of
> system() in the C library. To get around, use cmd.exe to parse the
> command.[/color]
Works like a charm !
New copy.php PHP script :
<?php
$cmd = '"' . dirname ($_SERVER ['PHP_SELF']) . '\mycopy.bat" /B "C:\test
copy\folder1\file1" "C:\test copy\folder2\file2"';
system ("cmd /C \"$cmd\"");
?>
New mycopy.bat :
copy %1 %2 %3
Thanks !
--
Jean-Marc. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,720 network members.
|