PHP:fopen 错误处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24753821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:27:28  来源:igfitidea点击:

PHP: fopen error handling

phperror-handlingfopen

提问by Fabian

I do fetch a file with

我确实获取了一个文件

$fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
$str = stream_get_contents($fp);
fclose($fp);

and then the method gives it back as image. But when fopen() fails, because the file did not exists, it throws an error:

然后该方法将其作为图像返回。但是当 fopen() 失败时,因为文件不存在,它会抛出一个错误:

[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...

This is coming back as json, obviously.

显然,这将作为 json 返回。

The Question is now: How can i catch the error and prevent the method from throwing this error directly to the client?

现在的问题是:如何捕获错误并防止该方法直接将此错误抛出给客户端?

回答by Cendak

You should first test the existence of a file by file_exists().

您应该首先通过 file_exists() 测试文件是否存在。

try
{
  $fileName = 'uploads/Team/img/'.$team_id.'.png';

  if ( !file_exists($fileName) ) {
    throw new Exception('File not found.');
  }

  $fp = fopen($fileName, "rb");
  if ( !$fp ) {
    throw new Exception('File open failed.');
  }  
  $str = stream_get_contents($fp);
  fclose($fp);

  // send success JSON

} catch ( Exception $e ) {
  // send error message if you can
} 

or simple solution without exceptions:

或无一例外的简单解决方案:

$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ) {

  $str = stream_get_contents($fp);
  fclose($fp);

  // send success JSON    
}
else
{
  // send error message if you can  
}

回答by Refazul Refat

You can use the file_exists()function before calling fopen().

您可以在调用fopen()之前使用file_exists()函数。

if(file_exists('uploads/Team/img/'.$team_id.'.png')
{
    $fp = fopen('uploads/Team/img/'.$team_id.'.png', "rb");
    $str = stream_get_contents($fp);
    fclose($fp);
}

回答by Sat93

Generically- This is probably the best way to do file-io in php (as mentioned by @Cendak here)

一般- 这可能是在 php 中执行 file-io 的最佳方法(如 @Cendak在这里提到的)

$fileName = 'uploads/Team/img/'.$team_id.'.png';
if ( file_exists($fileName) && ($fp = fopen($fileName, "rb"))!==false ){
    $str = stream_get_contents($fp);
    fclose($fp);
    // send success JSON    
}else{
    // send an error message if you can  
}

But it does not work with PHP 7.3, these modifications do,

但它不适用于 PHP 7.3,这些修改可以,

if(file_exists($filename) && ($fp = fopen($filename,"r") !== false)){
        $fp = fopen($filename,"r");
        $filedata = fread($fp,filesize($filename));
        fclose($fp);
}else{
        $filedata = "default-string";
}

回答by yyg

[{"message":"Warning: fopen(uploads\/Team\/img\/1.png): failed to open stream: No such file or directory in C:\...

the error is clear: you've put the wrong directory, you can try what you whant but it'll not work. you can make it work with this:

错误很明显:您放置了错误的目录,您可以尝试您想要的,但它不起作用。你可以让它工作:

  1. take your file and put it in the same folder of your php file (you'll be able to move it after don't worry, it's about your error) or on a folder "higher" of your script (just not outside of your www folder)
  2. change the fopen to ('./$team_id.'png',"rb");
  3. rerun your script file
  1. 把你的文件放在你的 php 文件的同一个文件夹中(不用担心,你可以移动它,这是关于你的错误)或你的脚本的“更高”文件夹(只是不在你的文件夹之外) www 文件夹)
  2. 将 fopen 更改为 ('./$team_id.'png',"rb");
  3. 重新运行你的脚本文件

don't forget this : you can't access a file that is'nt in your "www" folder (he doesn't found your file because he give you her name: the name come from the $team_id variable)

不要忘记这一点:您无法访问不在“www”文件夹中的文件(他没有找到您的文件,因为他给了您她的名字:名称来自 $team_id 变量)