Hello,

I am using php to check if a URL is available using th if file exists function.

if(file_exists("http://www.domain.jsp"))
{
$lines = file('http://www.domain.jsp');
}else{
$lines= echo $variable
}

But it keeps giving me errors...is there an alternative to if file exists?

Thanks

Hi.

This function works just fine.
Please post the errors.

Also you can use :

$url = 'http://www.example.com';
$handle = @fopen($url,'r');
if($handle !== false){
   echo 'Exists';
}
else{
   echo "Doesn't";
}

PS: What is this :

$lines= echo $variable

- Mitko Kostov

Hi Mitko...

What i am actually using th script for is to read some lines from one website and echo to another. The website i am echoing information from is usually not reliable. So if the url of the unreliable website is is not available, i dont want my website to be blank of have errors. So what i have decided to do is to write th info i wnt to a file...so if the unreliable website is down, mine will still have the last information i got from th site so here's what my script looks like
<?php
if(file_exists("http://www.domain.jsp")){
$lines = file('http://www.domain.jsp');

$datte=str_replace("<p class=\"para\"><font class=\"norm_text_white\">","",$lines[15]);
$datte=str_replace("</font></p>","",$datte);
$ind=str_replace("<p class=\"para\"><font class=\"norm_text\">","",$lines[30]);
$ind=str_replace("</font></td>","",$ind);
$val1=str_replace("<font class=\"norm_text\">","",$lines[33]);
$val1=str_replace("</font></p>","",$val1);
$fh = fopen("info.txt", 'w') or die("can't open file");
$stringData = $datte."<br>\n";
$stringData = $stringData.$val1."<br>\n";
fwrite($fh, $stringData);
fclose($fh);
echo $datte;
echo "<br><B>Info: </B>".$val1;
}
else{
$lines = file('http://info.txt');
}
?>

what usually happens is that the script just reads from the written file (info.txt) and doesnt do anything with the if file exsists part of the script. Pls, do u know an alternative???

Cheers
D

Hi.

This function works just fine.
Please post the errors.

Also you can use :

$url = 'http://www.example.com';
$handle = @fopen($url,'r');
if($handle !== false){
   echo 'Exists';
}
else{
   echo "Doesn't";
}

PS: What is this :

$lines= echo $variable

- Mitko Kostov

Hi Mitko...

What i am actually using th script for is to read some lines from one website and echo to another. The website i am echoing information from is usually not reliable. So if the url of the unreliable website is is not available, i dont want my website to be blank of have errors. So what i have decided to do is to write th info i wnt to a file...so if the unreliable website is down, mine will still have the last information i got from th site so here's what my script looks like
<?php
if(file_exists("http://www.domain.jsp")){
$lines = file('http://www.domain.jsp');

$datte=str_replace("<p class=\"para\"><font class=\"norm_text_white\">","",$lines[15]);
$datte=str_replace("</font></p>","",$datte);
$ind=str_replace("<p class=\"para\"><font class=\"norm_text\">","",$lines[30]);
$ind=str_replace("</font></td>","",$ind);
$val1=str_replace("<font class=\"norm_text\">","",$lines[33]);
$val1=str_replace("</font></p>","",$val1);
$fh = fopen("info.txt", 'w') or die("can't open file");
$stringData = $datte."<br>\n";
$stringData = $stringData.$val1."<br>\n";
fwrite($fh, $stringData);
fclose($fh);
echo $datte;
echo "<br><B>Info: </B>".$val1;
}
else{
$lines = file('http://info.txt');
}
?>

what usually happens is that the script just reads from the written file (info.txt) and doesnt do anything with the if file exsists part of the script. Pls, do u know an alternative???

Cheers
D

Hi again.

Have you tried with fopen ?

PS:

file_exists() : This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

Hello,

I am using php to check if a URL is available using th if file exists function.

if(file_exists("http://www.domain.jsp"))
{
$lines = file('http://www.domain.jsp');
}else{
$lines= echo $variable
}

But it keeps giving me errors...is there an alternative to if file exists?

Thanks

I don't believe file_exists() works on HTTP resources. It only works on the local filesystem.

Is: http://www.domain.jsp a typo? I don't believe .jsp is not a valid TLD.

Certain PHP filesystem functions such as file() have URL wrappers that allow it to access files over HTTP, I don't believe this is the case for file_exists() however.

-----------
Test:

$file = 'http://www.example.com/';
$resp = file_exists($file);
var_dump($resp); // (boolean) false

$resp is FALSE in both PHP4 and PHP5.
No HTTP Connection to example.com is made either, leading me to believe that there is not URL wrapper for HTTP for file_exists(). (The documentation is a bit vague on this)
--------------

You don't really have to test if the URL can be downloaded or not. The file function does this for you and returns false it if cant make the download.

Example:

$lines = file('http://example.com');
if ($lines !== false) {
// the download was ok, do your stuff
} else {
// there was a problem
}

note: !== tests for type as well as value. This is used so an empty string ' ' or string '0' is not interpreted as a FALSE.

If you want more fine grain control, you can use fopen() like suggested.

$fp = fopen('http://example.com', 'r');
if ($fp) {
	// connection was made to server at domain example.com
	fclose($fp);
}

Note that both file() and fopen() will trigger an notification (E_NOTICE level) error if the file you want on the domain does not exist. (ie: when a HTTP 404 Status is returned).
So you may want to suppress errors:

$fp = @fopen('http://example.com', 'r'); // @suppresses all error messages
if ($fp) {
	// connection was made to server at domain example.com
	fclose($fp);
}

OR

// suppress only notices and warnings, show other errors
$error_level = error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
$fp = fopen('http://example.com', 'r');
if ($fp) {
	// connection was made to server at domain example.com
	fclose($fp);
}
// reset to old error reporting level
error_reporting($error_level);

Hello All,

Problem has been solve...i used fopen...thnax a buch...who knows about VBA...i want it to pick values from an offline html page...i have absolutely no idea on how to start?

Cheers,
D

Hello All,
...i want it to pick values from an offline html page...

why not use xml
it is easier to handle

I think it's a good idea to run a few ping shots because some of the requests fail. I built a monitor that used fopen() and there were fail requests all the time.

You might want to use a ping class like the following:

hotscripts dot com/Detailed/11461.html

This way you're more sure about whether URL exists.

I hope that helps.
Tim

P.S. I didnt build this class, but whoever did it deserves all the credit :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.