2015-10-08 21 views
-1

我有以下函数来获取csv文件形成的第三方资源:Supressing fgetcsv警告

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); 
    } 

的问题出现时,第三方csv文件是不可用的,我得到这个错误:

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

测试此错误的最佳方法是什么?

+1

在使用'if'子句和布尔数据类型之后,花费一些时间研究。另外,请查看'fopen'函数的文档,特别是为什么它返回一个布尔值。 – Joshua

+0

当然在'fopen()'[或RTM](http://php.net/manual/en/function.fopen.php)后测试'$ file'变量 – RiggsFolly

回答

2

测试$file打开后。

$file = fopen($this->url, 'r'); 
if (!$file) { 
    die("Unable to open URL"); 
} 
+0

谢谢 - 这很明显,已经长时间的编码会话。 – HappyCoder