简体   繁体   English

打开文件夹而没有PHP警告

[英]Open folder without PHP warnings

opendir() gives me a PHP warning if a folder cannot be accessed. 如果无法访问文件夹, opendir()给我一个PHP警告。

I can use is_dir but what if the folder gets deleted in the meantime: 我可以使用is_dir,但是如果同时删除该文件夹怎么办:

  if(is_dir($some_dir)){
    // HERE, DELETED, GONE :(
    $d = opendir($some_dir);
    // warning !!0
  }

that warning messes up my entire error handling code. 该警告弄乱了我的整个错误处理代码。

Is there any way to do file manipulation without getting warnings? 有什么方法可以在没有警告的情况下进行文件操作? FALSE results are enough to understand that the file is inaccessible, I don't understand why does it have to warn me too :( FALSE结果足以了解该文件不可访问,我也不明白为什么它也必须警告我:(

Please dont tell me to use @. 请不要告诉我使用@。 There has to be a better way 一定有更好的方法

$d = @opendir($some_dir);

@ before a function silence errors the function could raise. @在函数沉默错误之前,函数可能会引发。

Then you can test $d to see if it worked or not (False if not working). 然后,您可以测试$d以查看其是否有效(如果无效则为False)。

Oh right, thanks comments, OP mentionned not to tell about @ my bad. 哦对了,谢谢您的评论,OP提到了@我的坏话。

Then another possibility is to change your php configuration so php doesn't raise warnings. 然后另一种可能性是更改您的php配置,以便php不会发出警告。

Try the following by checking if that variable is set in the first place. 通过首先检查该变量是否设置来尝试以下操作。

if(isset($some_dir)):
  if(is_dir($some_dir)):
    // HERE, DELETED, GONE :(
    $d = opendir($some_dir);
    // warning !!0
  endif;
endif;

Try this code 试试这个代码

  if(is_dir($some_dir)){
    // HERE, DELETED, GONE
    $d = @opendir($some_dir);
    // warning
  }

If you don't want to use '@' then you have to check/confirm your directory path on $some_dir is correct 如果不想使用“ @”,则必须检查/确认$ some_dir上的目录路径是否正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM