How to include only if file exists

前端 未结 10 804
深忆病人
深忆病人 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:29
    @include($file);
    

    Using at in front, ignores any error that that function might generate. Do not abuse this as IT IS WAY SLOWER than checking with if, but it is the shortest code alternative.

    0 讨论(0)
  • 2020-12-28 12:34
    <?php  
      if(file_exists('file.php'))
            include 'file.php';
        }else{
            header("Location: http://localhost/);
            exit;
    
        }
    
    0 讨论(0)
  • 2020-12-28 12:36

    the @ suppresses error messages.

    you cloud use:

    $file = 'script.php';
    if(file_exists($file))
      include($file);
    
    0 讨论(0)
  • 2020-12-28 12:39
    function get_image($img_name) {
        $filename = "../uploads/" . $img_name;
        $file_exists = file_exists($filename);
        if ($file_exists && !empty($img_name)) {
            $src = '<img src="' . $filename . '"/>';
        } else{
            $src = '';
        }
        return $src;
    }
    echo get_image($image_name);
    
    0 讨论(0)
  • 2020-12-28 12:50

    Check out the stream_resolve_include_path function.

    0 讨论(0)
  • 2020-12-28 12:51
    if(file_exists('file.php'))
        include 'file.php';
    

    That should do what you want

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