Page 1 of 1
clarification:PHP files
Posted: Fri Oct 17, 2008 4:24 am
by swetha
Code: Select all
$handle = fopen("http://www.google.com", "r");
if ($handle)
{
}
in the above statement,what does if($handle) mean? can u expand it for me?does it mean
if ($handle==true)?
if fopen is not successful,$handle returns null value.
thanks
swetha
Re: clarification:PHP files
Posted: Fri Oct 17, 2008 4:33 am
by aceconcepts
Yes, it basically means if file is able to open then hooray, lets read it.
Whenever you use a function such as fopen(), a value is returned (TRUE, FALSE or any other value depending what function you use).
Take a look at it in more detail here
http://uk3.php.net/fopen
Re: clarification:PHP files
Posted: Fri Oct 17, 2008 5:03 am
by swetha
Code: Select all
$str="http://www.google.com";
$handle = fopen($str, "r");
if ($handle)
{
}
in the above line fopen($str, "r"),how do i check if an invalid website is entered.
fopen should not be executed if an invalid site is opened.
eg for invalid $str variables.
$str="
http://wwww.google.com";
$str="
http:";
$str="
http://www.sgdjkag.com";
etc.
thanks for any help
Re: clarification:PHP files
Posted: Fri Oct 17, 2008 7:18 am
by aceconcepts
Validate the value of $str before you use it in fopen().
Something like:
Code: Select all
$str= "http://www.google.com";
if (preg_match("/^(http(s?):\/\/|ftp:\/\/{1})((\w+\.){1,})\w{2,}$/i", $str)) {
echo "URL is valid";
} else {
echo "URL is invalid";
}
Re: clarification:PHP files
Posted: Fri Oct 17, 2008 7:46 am
by Eran
Actually, the $handle variable in your code is a file pointer reference if the file was successfully opened, not a boolean TRUE.
The check is the kind of lazy PHP programming to validate such things, since most values are cast to a boolean TRUE except 0 and FALSE.
A more correct way to check it would be:
Re: clarification:PHP files
Posted: Fri Oct 17, 2008 8:39 am
by aceconcepts
Good correction - I was being lazy and didn't even check
