PHP包含文件 - 在功能中覆盖自身

时间:2016-04-02 13:19:30

标签: php include include-path

这是我的代码:

<?php


     if(isset($_POST['load_more'])){
	
     	$variable = $_SESSION['variable']++;
     	generateNewPage($detail_locator, $variable);
     	
    }
    
    function generateNewPage($detail_locator, $variable){
    

    	        $relative_path = "load_more/newfile $variable .php";
		$myfile = fopen($relative_path, "a") or die("Unable to open file!");
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		fclose($myfile);
		
		include "load_more/newfile $variable .php";
		
    
    }
    

?>
<div id="reload_section">
</ul>
<center><br />
	<form  method="post">
		<input type="submit" value="Load More" name="load_more" class="load_more_content" />
	</form>
</center>
</div>

基本上,每次用户点击按钮时,都会通过每次向fopen添加1的会话生成新页面。

当用户点击按钮时,会生成一个页面,然后包含该页面。

但是,每次运行该函数时,include语句都会覆盖自身,并且只包含最新生成的页面。

我希望在用户点击按钮时逐步包含每个生成的页面。

这可能吗?如果是这样的话怎么样?

@David Jones的更新:

<?php

	if (!isset($_SESSION['files'])) {
    	$_SESSION['files'] = []; // or array() if your PHP version is < 5.4
	}

	if(isset($_POST['load_more'])){
	
     		$variable = $_SESSION['variable']++;
     		generateNewPage($detail_locator, $variable);
     	
    }
    
    function generateNewPage($detail_locator, $variable){
    

    	$relative_path = "load_more/newfile $variable .php";
		$myfile = fopen($relative_path, "a") or die("Unable to open file!");
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);

		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		$new_code = "<li><br /><?php kal_generator($detail_locator); ?></li>";
		$txt = $new_code;
		fwrite($myfile, $txt);
		
		fclose($myfile);
		
		$SESSION['files'][] = "load_more/newfile $variable .php";
		
		foreach ($_SESSION['files'] as $file) {
    		include $file;
		}
		
    
    }
    

?>
<div id="reload_section">
</ul>
<center><br />
	<form  method="post">
		<input type="submit" value="Load More" name="load_more" class="load_more_content" />
	</form>
</center>
</div>

1 个答案:

答案 0 :(得分:0)

你应该有一个数组来跟踪文件路径,函数应该添加文件路径到数组。然后在同一个函数中或最好是一个单独的函数,你将遍历数组并包含每个文件。

所以在你的函数之外你会定义一个新数组,这里我在会话中这样做,因此可以很容易地访问它。

if (!isset($_SESSION['files'])) {
    $_SESSION['files'] = []; // or array() if your PHP version is < 5.4
}

现在在你的函数中改变这一行:

include "load_more/newfile $variable .php";

对此:

$_SESSION['files'][] = "load_more/newfile $variable .php";

现在循环遍历files数组并包含每个数组。这可以在generateNewPage函数中完成,也可以在单独的函数中完成,但是可以从generateNewPage函数调用。

foreach ($_SESSION['files'] as $file) {
    include $file;
}

希望这很清楚。如果您有任何问题,请告诉我。