1

我正在尝试包含一个文件(如果存在),如果该文件不存在,我希望它包含另一个文件。

我有以下似乎可以正常工作的代码,我的代码的唯一问题是如果文件确实存在,那么它会同时显示它们。

我只想包括

包含/article.php

如果

包括/'.$future.'.php

不存在。

如果

包括/'.$future.'.php

存在我不想包括

包含/article.php

<?php
$page = $_GET['include'];
if (file_exists('include/'.$future.'.php')){
   include('include/'.$future.'.php');
}
else{
  include('include/article.php');
 } 
?>
4

2 回答 2

2

您的变量 $future 甚至从未定义过,那么它是如何被包含在内的?我认为您的意思是 $page 和 $future 是同一个变量。此外,在获取请求中包含用户指定的文件一开始并没有多大意义,但也可能存在安全风险。

于 2013-08-08T23:27:05.527 回答
0

也许这就是你需要的?

确保在使用变量之前定义您要 使用的任何变量...

<?php
$page = ''; // show emptyness is there is nothing dome below 
$page = isset($_GET['include']); // Do you have a variable in your URL that IS called "include" and is the include variable set? for example: http://www.example.com/index.php?include=.......  you might want to check for empty value and write a default value  as well but I didn't here ;)
// REMEMBER: $future must be defined BEFORE this line!
if (file_exists('include/' . $future . '.php') && is_file('include/' . $future . '.php')){ // file exists on server: true/false and is it a file or a directory? If Directory it will NOT be included as I used is_file()!
   include('include/' . $future . '.php');
   }elseif{
       (file_exists('include/article.php') && is_file('include/article.php')){
           include 'include/article.php';
       }else{
           echo 'WOW, No article found! You just found a error...<br />We will repair all errors ASAP! Thank you for visiting us...';
       }
?>
于 2016-09-23T22:31:21.923 回答