Php 在使用fwrite()编写文件时,如何将一个页面中的变量包含在另一个页面中?

Php 在使用fwrite()编写文件时,如何将一个页面中的变量包含在另一个页面中?,php,mysql,html,forms,fwrite,Php,Mysql,Html,Forms,Fwrite,代码如下: <?php // For use in creating individual page $tpl_file = "submission.php"; $tpl_path = "templates/"; $submissions_path = "submissions/"; // For use in querying submitter name $username = $_GET['username']; session_start(); $_SESSION['use

代码如下:

<?php
// For use in creating individual page
$tpl_file = "submission.php";
$tpl_path = "templates/";
$submissions_path = "submissions/";



// For use in querying submitter name

$username = $_GET['username'];
session_start();
$_SESSION['username'] = $username; 

//Database Information

$dbhost = "";
$dbname = "";
$dbuser = "";
$dbpass = "";

//Connect to database

mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());


$name = $_POST['name'];
$filename = $_POST['filename'];   
$submitter = $username;
list($width, $height) = getimagesize("$filename");
$type = exif_imagetype($_POST['filename']);

$checkuser = mysql_query("SELECT filename FROM images WHERE filename='$filename'");

$filename_exist = mysql_num_rows($checkuser);

if($filename_exist > 0){
    echo "I'm sorry but this image has already been submitted. Please feel free to try another.";
    unset($filename);
    include 'upload.php';
    exit();
}


if (exif_imagetype($_POST['filename']) == IMAGETYPE_GIF) {
    echo "Sorry, but we can't accept GIFs. Please feel free to try uploading another.";
    unset($filename);
    include 'upload.php';
    exit();
}



$query = "INSERT INTO images (name, filename, submitter, width, height, type)
VALUES('$name', '$filename', '$submitter', '$width', '$height', $type)";
mysql_query($query) or die(mysql_error());
mysql_close();

echo "Thanks for your submission!<br/> Upload another <a href='/~lyons/upload.php'>here</a>!";

$tpl = file_get_contents($tpl_path.$tpl_file);
$php_file_name = $name.".php";

$fh = fopen($submissions_path.$php_file_name, "w");
fwrite($fh, $tpl);
fclose($fp); 
?>
查找PHP

它是PHP的一个内置功能,用于您正在做的事情


会话以每个用户为单位存储数据,因此如果您希望其他人看到变量,则必须使用数据库或保存到文件。

我建议使用字符串,如
%name%
%username%
等来标记变量的占位符

然后,在写入文件之前,请尝试以下操作:

$tpl = preg_replace("(%([a-z_][a-z0-9_]*)%)ie",'$$1',$tpl);

例如,这将找到
%filename%
,并将其替换为变量
$filename

的内容,我想我有点理解会话,但这不是更能显示有人登录或其他什么吗?如果他们去提交另一张图片,那么前一张图片中的数据会发生什么变化?我想我误解了您想要存储什么样的信息。你应该使用一个数据库来存储上传图片及其上传者的路径。我需要,我只需要一种方法,让模板文件为个人提交输入正确的细节。如果我不能携带任何变量,那么我就不能告诉它我想从数据库中获取什么。你建议我把$tpl变量改成这个吗?因为我不懂这些代码。不是你的错,我还在学习。在模板文件中,用我建议的
%varname%
表单替换所有的PHP小片段。然后在调用
fwrite()
之前添加该行代码。从那以后应该没问题。我想我做错了。在模板文件中,我将
放在希望显示$name变量的位置。然后,我将$tpl变量的值替换为您给我的值。文件不再显示任何模板,所以我哪里出错了?谢谢你到目前为止的帮助。好的,让我们一步一步来。您不应该替换大代码文件中的任何内容,只添加了那一行。撤消对代码文件(不是模板文件,保持原样)所做的任何更改,然后在定义
$tpl
之后,但在写入文件之前,在某处添加代码行,看看会发生什么。好的,我在$tpl的第一个值之后添加了它,所以现在我有了$tpl的两个值:
$tpl=file\u get\u contents($tpl\u path.$tpl\u file); $tpl=preg_replace((((([a-z_u][a-z0-9_]*)),即“,$1',$tpl)我保留了所有其他内容,包括模板文件,现在我又没有为变量填充任何内容。对不起,如果我有点难对付,我只是在过去几周学习PHP,所以我还是一个新手。
$tpl = preg_replace("(%([a-z_][a-z0-9_]*)%)ie",'$$1',$tpl);