failed to open stream (php)

Post September 5th, 2005, 10:27 pm

I am using the following code for joining an external file to my document.

$page = join("", file("http://".$url))

It works fine, when it is able to open the file.
But when the file does not exist then it shows

Warning: file(http://www.tennessee.gov/mental/): failed to open stream: Bad file descriptor in D:\Work\mailgrabber\add_mail.php on line 8

Is there anyway that I can show my own message here?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post September 5th, 2005, 10:27 pm

  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 29, 2004
  • Posts: 468
  • Loc: India
  • Status: Offline

Post September 5th, 2005, 10:36 pm

Try this out.

$page = join("", file("http://".$url)) or die("Your Message Goes Here");
Amit
My Blog http://www.amityadav.name

Post September 5th, 2005, 11:14 pm

Thanks for your reply.
Actually there is different code for both the cases. So is there can any kind of 'if' condition for it? I tried to use 'if' but it didn't work. Any Ideas?

Post September 5th, 2005, 11:37 pm

The problem with using file() on a remote URL is that even if the file you requested doesn't exist, the server will generally return a 404 file.
This actually returns data that join() can use, and will not normally report an error or a warning, though it will not be the data you are expecting.

I'm curious about your version of php. If you have the fopen wrappers enabled, file() should (almost) never return a warning or error, since the target server should return a 404 if the original file isn't available.

Have you ever gotten the remote file to open? If you have, and it just started returning false, it could be that the remote server is blocking you.
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 29, 2004
  • Posts: 468
  • Loc: India
  • Status: Offline

Post September 5th, 2005, 11:43 pm

join() and Implode() returns string they do not return boolean, so you have to check the length of the string that is returned.

PHP Code: [ Download ] [ Select ]
$page = join("", file("http://".$url));
 
 
 
if(strlen($page) <= 0)
 
{
 
    echo "Error Message here";
 
}
  1. $page = join("", file("http://".$url));
  2.  
  3.  
  4.  
  5. if(strlen($page) <= 0)
  6.  
  7. {
  8.  
  9.     echo "Error Message here";
  10.  
  11. }


:lol:
Amit
My Blog http://www.amityadav.name

Post September 5th, 2005, 11:53 pm

his problem is that file() is returning false.
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 29, 2004
  • Posts: 468
  • Loc: India
  • Status: Offline

Post September 6th, 2005, 12:03 am

I am modifying my last code a bit:


PHP Code: [ Download ] [ Select ]
$fileArray = file("http://".$url);
 
if(is_array($fileArray) || @count($fileArray) > 0)
 
{
 
    $page = join("", $fileArray);
 
 
 
   if(strlen($page) <= 0)
 
  {
 
      echo "Error Message here";
 
  }
 
}
 
else
 
{
 
    echo "Show your error here":
 
}
  1. $fileArray = file("http://".$url);
  2.  
  3. if(is_array($fileArray) || @count($fileArray) > 0)
  4.  
  5. {
  6.  
  7.     $page = join("", $fileArray);
  8.  
  9.  
  10.  
  11.    if(strlen($page) <= 0)
  12.  
  13.   {
  14.  
  15.       echo "Error Message here";
  16.  
  17.   }
  18.  
  19. }
  20.  
  21. else
  22.  
  23. {
  24.  
  25.     echo "Show your error here":
  26.  
  27. }


Hope this works now.. :) 8)
Amit
My Blog http://www.amityadav.name

Post September 6th, 2005, 12:14 am

This is the code I'm using
Code: [ Download ] [ Select ]
<?PHP
      if (file("http://www.tennessee.gov/mental/"))
        {
        $page = join("", file("http://www.tennessee.gov/mental/"));
        echo "Page Available";
        }
      else
        {
         echo "Page is not available";
        }

?>
  1. <?PHP
  2.       if (file("http://www.tennessee.gov/mental/"))
  3.         {
  4.         $page = join("", file("http://www.tennessee.gov/mental/"));
  5.         echo "Page Available";
  6.         }
  7.       else
  8.         {
  9.          echo "Page is not available";
  10.         }
  11. ?>


and the error returned was :

Code: [ Download ] [ Select ]
Warning: file(http://www.tennessee.gov/mental/): failed to open stream: Bad file descriptor in D:\Work\test\test.php on line 2
Page is not available
  1. Warning: file(http://www.tennessee.gov/mental/): failed to open stream: Bad file descriptor in D:\Work\test\test.php on line 2
  2. Page is not available


and when i manually checked the webpage , the error returned was "Cannot Find Server- The page cannot be displayed"

Is there any way I can stop it from showing its own error.(Warning: file(http://www.tennessee.gov/mental/): failed to open stream: Bad file descriptor in D:\Work\test\test.php on line 2
)

Post September 6th, 2005, 12:23 am

put an @ sign in front of the file call
PHP Code: [ Download ] [ Select ]
<?PHP
 
            if (@file("http://www.tennessee.gov/mental/"))
 
                {
 
                $page = @join("", @file("http://www.tennessee.gov/mental/"));
 
                echo "Page Available";
 
                }
 
            else
 
                {
 
                  echo "Page is not available";
 
                }
 
 
 
?>
  1. <?PHP
  2.  
  3.             if (@file("http://www.tennessee.gov/mental/"))
  4.  
  5.                 {
  6.  
  7.                 $page = @join("", @file("http://www.tennessee.gov/mental/"));
  8.  
  9.                 echo "Page Available";
  10.  
  11.                 }
  12.  
  13.             else
  14.  
  15.                 {
  16.  
  17.                   echo "Page is not available";
  18.  
  19.                 }
  20.  
  21.  
  22.  
  23. ?>


The "Bad File Descriptor" error message means you don't have fopen wrappers enabled on your localhost. It *probably* isn't like that on your server, but using the @ symbol in front of almost ANY php function will silence it's errors. In this case, though, you'll get 404 errors mixed in with your content if fopen wrappers are enabled on your server. On your localhost, you should get "Page is not available"

Post September 6th, 2005, 12:54 am

It worked !

Thanks to both Amit and ScienceOfSpock.

Post September 6th, 2005, 12:58 am

BTW, Is Sharandeep your given name? It sounds really cool :)

Post September 6th, 2005, 1:09 am

:)

Yes, it is my given name.

Post Information

  • Total Posts in this topic: 12 posts
  • Users browsing this forum: No registered users and 451 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2010 Unmelted, LLC. Driven by phpBB © 2010 phpBB Group.