php - 静默捕获php "file_get_contents"错误

标签 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 />');
}

当 file_get_contents = true(无错误)时,一切正常 当 file_get_contents = false 时,我只是在屏幕上收到大错误消息...我只是想测试它,而不是执行它!

怎么做?

这是错误信息:

[function.file-get-contents]: failed to open stream: Inappropriate ioctl for device in 

最佳答案

快速方法:

if( false == (@file_get_contents($jsonaddress)))

'@' 抑制错误。

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

if (! file_exists($jsonaddress)){

可能会做你想做的事(看看你是否能得到流,但如果失败则简单地返回 false)...但我不确定它的效果如何。 (最近没有用 fopen 包装器尝试过)

关于php - 静默捕获php "file_get_contents"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1507224/

相关文章:

php - 只计算mysql表中不同的值?

PHP:MVC 用户身份验证和全局用户对象?

PHP Bootstrap 在本地主机上完美工作,但在 VPS 上无法工作

php - Guzzle 池异步请求范围

php - 将多个值与多个值进行比较

php - 使用 PHP 和 C/C++ 进行 eclipse

php 创建文件并作为附件发送而不实际创建文件

php - 有什么原因这行不通吗?

php - 响应式设计 - 数据库生成的图标/图像

php - 我正在尝试创建动态表,其中将获取数据并且我可以编辑我想要的任何值