-1

I have the following function to get a csv file form a third party resource:

    public function getSubject()
    {       
        $file   =  fopen( $this->url , 'r');
        $result = array();
        while ( ( $line = fgetcsv( $file )) !== false ) {
            if ( array(0 => null) !== $line ) {
                $result[] = $line;
            }
        }

        fclose( $file );
        $this->setSubject( $result );
    }

The problem arises when the third party csv file is unavailable, I get this error:

PHP Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /var/www/html/code.php on line 57

What is the best way to test for this error?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
HappyCoder
  • 5,985
  • 6
  • 42
  • 73
  • 1
    invest some time in researching after using the `if` clause and the boolean data type. also, look at the documentation of the `fopen` function, especially why it returns a boolean value. – Joshua Oct 08 '15 at 10:49
  • Well test the `$file` variable of course after the `fopen()` [Or R.T.M](http://php.net/manual/en/function.fopen.php) – RiggsFolly Oct 08 '15 at 10:52

1 Answers1

2

Test $file after you open it.

$file = fopen($this->url, 'r');
if (!$file) {
    die("Unable to open URL");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612