hatrickpatrick Posted August 23, 2007 Share Posted August 23, 2007 I found this function in a php.net discussion, it checks to see whether a url exists or not. I REALLY need to get this working for my site, and it just about does - problem is, if the URL has a space in it, it always returns false, even if the URL does actually exist. Can anyone figure out how I can make it "spaces compliant"? I've tried changing the space in my $url variable to a + and to a %20, niether worked... function url_exists($url) { $a_url = parse_url($url); if (!isset($a_url['port'])) $a_url['port'] = 80; $errno = 0; $errstr = ''; $timeout = 30; if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){ $fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout); if (!$fid) return false; $page = isset($a_url['path'])?$a_url['path']:''; $page .= isset($a_url['query'])?'?'.$a_url['query']:''; fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n"); $head = fread($fid, 4096); fclose($fid); return preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head); } else { return false; } } Help would be very much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/ Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 Try print_r($a_url) after the line $a_url = parse_url($url), that function is probably the problem. See if that prints out the right values with spaces. Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/#findComment-332276 Share on other sites More sharing options...
hatrickpatrick Posted August 23, 2007 Author Share Posted August 23, 2007 No it printed correctly, with the spaced filename after [path]. Strangely, it printed the URL twice, could that be the problem? Anything else I could try? Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/#findComment-332292 Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 Could you post the full URL you are trying to use. That function seems a little akward. You could do exactly what that is doing a lot simpler with something like: $url = get_headers($url); if (ereg("200", $url[0])) echo "it is a url!"; It doesn't check if the url is an ip, like that function does, but it certainly works to find if a url exists. Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/#findComment-332347 Share on other sites More sharing options...
trq Posted August 23, 2007 Share Posted August 23, 2007 URLs cannot contain spaces. Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/#findComment-332375 Share on other sites More sharing options...
hatrickpatrick Posted August 23, 2007 Author Share Posted August 23, 2007 So then, if I upload a file to my server which has a space in it, how is the space represented in PHP? Like I said, I tried %20 and + but they didn't work... I'll try that function though, might be easier... Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/#findComment-332398 Share on other sites More sharing options...
lemmin Posted August 23, 2007 Share Posted August 23, 2007 They are represented as %20. PHP will read that. When a form submits it changes most invalid characters to their % representation. If you are manually making the link, just put a %20 where the space should be. The code I posted should work fine with that. You can use url_decode to change them back again. http://us.php.net/manual/en/function.urldecode.php Quote Link to comment https://forums.phpfreaks.com/topic/66391-url_exists-function-how-can-i-get-it-to-recognize-spaces/#findComment-332401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.