Page 1 of 1

fopen on a file/url with blanks/whitespaces

Posted: Fri Jul 18, 2008 5:42 am
by alfmarius
Hi :)

I just stumbled upon this:

search.php:

Code: Select all

<?php
print $_GET['arg'];
?>
test.php:

Code: Select all

<?php
print "a) " . file_get_contents("http://server/search.php?arg=oneword", "r");
print "b) " . file_get_contents("http://server/search.php?arg=two words", "r");
?>
test.php outputs:

Code: Select all

a) oneword
b) Warning: file_get_contents(http://server/search.php?arg=two words) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in ...
I can live with the fact that _this_is_how_it_is_ and do some query replace on whitespaces, but before I implement that scheme I just wanted to check if anyone know if there is a way to make this work directly? (I have seen %20 used alot for whitespaces, without really understanding why, is this why?)

Re: fopen on a file/url with blanks/whitespaces

Posted: Fri Jul 18, 2008 7:37 am
by lonelywolf
my output from your code:
a) onewordb) two

--------------------
in php manual, space is not valid in parameter.
http://php.net/file_get_contents

Re: fopen on a file/url with blanks/whitespaces

Posted: Fri Jul 18, 2008 7:40 am
by dhrosti
I have seen %20 used alot for whitespaces, without really understanding why, is this why?
this is encoded whitespace, you find similar characters with colons and ampersands, etc.

two functions in php can be used to encode/decode the urls, strange as it might sounds, they are called urlencode() and urldecode() respectively.

Re: fopen on a file/url with blanks/whitespaces

Posted: Mon Jul 21, 2008 3:43 am
by alfmarius
Thanks lonelywolf and dhrosti for your interest!
lonelywolf wrote:my output from your code:
a) onewordb) two
Hm, strange.. What version of php are you using? I'm using 5.2.0-8

I have come to the conclusion that whitespace in urls are generally not tolerated, and will lead to unexpected results. Why it is so difficult to make it work with urls i cannot tell.

I noticed that when using the plus sign "+" in the url, php will replace it with whitespace, so i guess this is the solution.

But I couldn't find this behavior documented anywhere in the php docs.. Does someone know if this is documented so I can treat it as a fact?

Code: Select all

http://server/file.php?arg=one+two+three
$_GET = "one two three"