2014-01-13 73 views
-1

我有以下警告应该在那里,但有没有办法阻止他们写入页面而不实际禁用全局警告?PHP停止警告1行代码

警告:

警告:的file_get_contents(E:/connected.txt):未能打开流: C中没有这样的文件或目录:\ XAMPP \ htdocs中\ PPA \上线的test.php 19

警告:的file_get_contents(F:/connected.txt):未能打开流: 在C无这样的文件或目录:\ XAMPP \ htdocs中\ PPA \ test.php的第19行

警告: file_get_contents(G:/connected.txt):无法打开流: 否第19行的C:\ xampp \ htdocs \ ppa \ test.php中的uch文件或目录

警告:file_get_contents(H:/connected.txt):未能打开流: C: \ XAMPP \ htdocs中\ PPA上线\ test.php的19

警告:的file_get_contents(I:/connected.txt):未能打开流: C中没有这样的文件或目录:\ XAMPP \ htdocs中\ PPA \上线19 test.php的

警告:的file_get_contents(Y:/connected.txt):未能打开流: 在C无这样的文件或目录:\ XAMPP \ htdocs中\ PPA \上线19

test.php的

警告:的file_get_contents(K:/connected.txt):未能打开流: 在C无这样的文件或目录:\ XAMPP \ htdocs中\ PPA \ test.php的第19行

警告:的file_get_contents( L:/connected.txt):无法打开流: 第19行中的C:\ xampp \ htdocs \ ppa \ test.php中没有此文件或目录

警告:file_get_contents(M:/connected.txt) :未能打开流: 第19行中的C:\ xampp \ htdocs \ ppa \ test.php中没有此文件或目录

警告:file_get_contents(N:/connected.txt):未能打开流: 在C无这样的文件或目录:\ XAMPP \ htdocs中\ PPA \ test.php的第19行

警告:的file_get_contents(O:/connected.txt):未能打开流: 没有这样的文件或在目录C:\ XAMPP \ htdocs中\ PPA \ test.php的第19行

警告:的file_get_contents(P:/connected.txt):未能打开流: 在C无这样的文件或目录:\ XAMPP \ htdocs中\ PPA \上线test.php的19

PHP功能:

//Check if the connection file is present on each drive 
function scanDrives() { 
    //Possible drives 
    $letters = "DEFGHIJKLMNOP"; 
    $letters = str_split($letters); 

    $code = md5("FMbHSBTMTXhu3TWp"); 

    //Check for a certain file on each device 
    foreach($letters as $x) { 
     $file = file_get_contents($x.":/connected.txt"); //This is what causes the error as the file can't be found. 
     if ($file != false) { 
      $file_code = split(":", $file); 
      //Check if the file has the correct pass code 
      if($file_code[0] == $code) { 
       //Successful, return pass value and device letter and set name 
       echo (1).",".$x.",".$file_code[1]; 
      } 
     } 
    } 
} 

谢谢。

回答

4

不要盲目地用file_get_contents打开文件。检查它是否先存在。

if(file_exists($x.":/connected.txt")){ 
    $file = file_get_contents($x.":/connected.txt"); 
    // ... 
} 
+1

这比使用'@'抑制更好。 +1 – naththedeveloper

+0

@FDL:我会*从不*建议'@'操作符。它总是*更好地检查/处理错误,而不是盲目地忽略它们:-) –

1

我同意@Rocket危险品,

但回答你的问题 - 在PHP中您可以使用Error Control operators ..具体来说,你可以用@符号来忽略错误。

简单的例子

$file = @file_get_contents("file_doesnt_exist.php"); // wont throw any errors. 
+1

我真正使用@操作符的唯一时间是使用DOMXpath来避免其他人的文档中出现xml/xhtml错误。知道这一点很有用,但总是更好地解决错误,而不是将它们推到地毯下。 – codeaddict