LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-30-2007, 06:19 PM   #1
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
How do I check if the php function file() fails?


I am using the file() function in PHP to grab the source of a URL. The problem I am having is that every once in a while file() will randomly fail to open the URL and throw a "failed to open stream" error. Is there a simple way I can check for this and retry the file() function if it does fail?

Thanks,
Farmerjoe
 
Old 11-30-2007, 06:25 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 98
According to: http://us.php.net/file

Return Values

Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached. Upon failure, file() returns FALSE.

So, if the value returned is false, get it again.

HTH

Forrest
 
Old 11-30-2007, 06:42 PM   #3
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
well i tried this but it didn't work. :

while(!$lines)
{
$lines = file($letterurl);
}

I still get a failed message everyonce in a while. Is there a better why to retry if it fails?
 
Old 12-01-2007, 06:40 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.2, Centos 5.8
Posts: 11,740

Rep: Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905Reputation: 905
Try different logic:

NB: pseudo-logic; i'm not a PHP programmer.
if( file($letterurl) )
then
$lines = file($letterurl);
else
log/email an error
fi
 
Old 12-01-2007, 09:16 PM   #5
pafinator11
Member
 
Registered: Jul 2005
Location: Massachusetts
Distribution: Debian Lenny, FreeBSD
Posts: 89

Rep: Reputation: 15
I would try:

Code:
$lines = "false";

while($lines == "false" && $attempts < 5){

$lines = file($letterurl);

$attempts + 1;
}
This will give it five tries before it quits.
 
Old 12-02-2007, 04:36 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 237Reputation: 237Reputation: 237
Code:
<?php

while (1) 
{
 status = file(..);
 if ( status )
 {
   break;
 }
}

?>
 
Old 12-02-2007, 09:17 AM   #7
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
Thanks guys!

I began just doing this:

Code:
$lines = file($URL);

while(!$lines)
{
       file($URL);
}
But obviously, if I ever did get a bad URL, it would be in an endless loop.
pafinator11, your solution solves that problem quite well. I thank all you guys for your help! I can always count on the community here!

Last edited by farmerjoe; 12-02-2007 at 09:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
App intallation fails in Wine: Check your pif file Arjuna Linux - Software 2 12-07-2006 01:20 AM
Can I run localhost/file.php with CRON function kondor5 Linux - General 1 03-28-2004 06:44 AM
cannot login because file system check fails because of bad magic number trutnev Linux - Newbie 1 03-23-2004 06:44 PM
File-system check on boot fails-- ALSA hang vivalinux Mandriva 6 11-25-2003 08:24 AM
File Permission Error in PHP fopen() function tuka Linux - Software 2 08-12-2003 08:16 PM


All times are GMT -5. The time now is 05:05 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration