Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to bypass warning message in fopen?

Status
Not open for further replies.

weifan

IS-IT--Management
Sep 6, 2003
45
US
Hi,

I use fopen to get data from a website with different ID number.

for ($count = 1; $count <= 10; ++$count) {
$url=&quot;preg_match(&quot;/^(https?:\/\/)?([^\/]*)(.*)/i&quot;, &quot;$url&quot;, $matches);
$domain = &quot; . $matches[2];
$page = $matches[3];
$fd = fopen($domain.$page, &quot;rb&quot;);
$code = &quot;&quot;;
while(!feof($fd)){
$code .= fread($fd, 1000);
}
fclose($fd);

However, I get warning messages and the program will not stop or need to wait for a long long time, if the id is invalid.

How can I bypass the loop if the id in not valid?

Thanks,

Roger
 
I'm not quite sure what you mean by bypass the loop, what you can do is prefxi the fopen with a @ i.e $fd=@fopen(.. This stops PHP reporting the error, you will have to handle it, your code will look something like:

$fd=@fopen($domain....);
if ($fd == NULL) {
echo &quot;not found&quot;;
else
{
you app code;
}

Check the manual for what comes back from fopen on a bad open. I think you alos need url in fopen switched on in the php.ini file or at run time, back to the manual again !
 
As a good rule, use of the &quot;@&quot; error-suppression operator should be avoided.

In the case of errors with fopen(), use of functions like file_exists() ( is_readable() ( and is_writable() ( can be used to gracefully catch errors rather than just stopping them from being displayed.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top