11 February 2008

php popen whitespace bug in windows

This popen path with whitespace bug, http://bugs.php.net/bug.php?id=40988&edit=1 make my day worse. Tired from long 9 hours journey from 'balik kampung', arrive at 3.30am and 8.30am go to office, huh. Then have to debug why the stupid php don't want execute the command.

I want use php to backup a postgis table. So

system('"C:\\Program Files\\PostgreSQL\\8.2\\bin\\pg_dump.exe" -t project_geom projectgis > '.$backup);


that, should straight forward, but the output is empty page. Then use popen to redirect the stdout and stderr, 2>&1 for debugging, I get this message

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


Tried many technique to solve: escapeshellarg(), backslash to slash, all lowercase, etc, then afters spent hours debugging, it was the php problem, not my code. grrr.

BTW:
A 8.3 name (PROGRA~1) will still work on Windows, if it's not disabled
in the NTFS settings.

6 comments:

Anonymous said...

What PHP version are you using? It works fine on my box.

Foobaz said...

php version 5.2.1

Anonymous said...

I'm using 5.2.4. I've used spaces in the exec family of calls, not had a problem so far. Of course, all on *nix systems (perhaps the behaviour is different on Windows).

Foobaz said...

Yeah, I guess this problem for windows only, anyway try this snippet to reproduce the bug

---

$host = escapeshellarg('localhost');
// $host = 'localhost';

$command = "\"c:\\Program Files\\PostgreSQL\\8.2\\bin\\psql.exe\" -h $host --version 2>&1";
// $command = "c:\\progra~1\\PostgreSQL\\8.2\\bin\\psql.exe\" -h $host --version 2>&1";

$handle = popen($command, 'r');
$read = fread($handle, 2096);

print_r($command);
print_r($read);
pclose($handle);

---

The problem happen when escape the argument using quote. The commented line is the working one. Tested on php version 5.2.5

yoins said...

Express Your Unique Fashion Statement w/ Our Talent Designer Clothing! Fahion & Well-Crafted Clothing Should Not Be Expensive w/ Our Top-Level Supply Chain! Styles: Sexy, Elegant, Casual, Basic, Cocktail, Formal.

Buy Essay said...

Write your essay online. BuyEssayFriend creates discipline essays among them: research papers, personal and law essays, thesis, and academic writings.