静默捕获php"file_get_contents"的错误

men*_*mam 1 php

这是代码:

if( false == (file_get_contents($jsonaddress)))
{
    //error
    print ('Error with stream, getting file instead !<br />');
    $jsonaddress = 'listedesodeurs.txt';
} 

else 
{
    //noerror
    print ('Sucessfully GET data from JSON stream<br />');
    $jsoncontent = file_get_contents($jsonaddress);
    $size = file_put_contents('listedesodeurs.txt', $jsoncontent);
    echo ('Making backup of stream in file : '.round(($size/1024),0).' KB <br />');
}
Run Code Online (Sandbox Code Playgroud)

当file_get_contents = true(没有错误)当file_get_contents = false时,一切都很顺利我juste在屏幕上收到了大错误消息...我只想测试它,而不是执行它!

怎么样 ?

这是错误消息:

[function.file-get-contents]: failed to open stream: Inappropriate ioctl for device in 
Run Code Online (Sandbox Code Playgroud)

tim*_*dev 10

快捷方式:

if( false == (@file_get_contents($jsonaddress)))
Run Code Online (Sandbox Code Playgroud)

'@'抑制错误.

一种可能更好的方法是测试:

if (! file_exists($jsonaddress)){
Run Code Online (Sandbox Code Playgroud)

也许你能做到你想要的(看看你是否可以获得流,但如果它失败则简单地返回false)......但我不确定它会有多好用.(最近还没试过fopen包装)