How to include only if file exists

前端 未结 10 805
深忆病人
深忆病人 2020-12-28 12:24

I need an include function / statement that will include a file only if it exists. Is there one in PHP?

You might suggest using @include bu

相关标签:
10条回答
  • 2020-12-28 12:51

    Try using file_exists()

    if(file_exists($file)){
      include $file;
    }
    
    0 讨论(0)
  • 2020-12-28 12:52

    Based on @Select0r's answer, I ended up using

    if (file_exists(stream_resolve_include_path($file)))
        include($file);
    

    This solution works even if you write this code in a file that has been included itself from a file in another directory.

    0 讨论(0)
  • 2020-12-28 12:53

    I think you have to use file_exists, because if there was such an include, it would have been listed here: http://php.net/manual/en/function.include.php

    0 讨论(0)
  • 2020-12-28 12:54

    How about using file_exists before the include?

    0 讨论(0)
提交回复
热议问题