为每次提交创建一个新页面- PHP错误


Creating a new page for each submission - PHP errors

网址:

我有一个提交页面,和一个表单操作的页面,查询提交信息到我的数据库。我将在下面包含该代码。我想做的是,让它为每个提交创建一个单独的页面。然而,当我尝试上传时,我得到了大量的错误。它上传,但它肯定不会创建新的页面。我有一个模板表单,我会给你看,但首先,这是上传页面:

<?php
// For use in creating individual page
$tpl_file = "submission.php";
$tpl_path = "/~lyons/templates/";
$submissions_path = "/~lyons/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>!";
$placeholders = array("{name}", "{filename}", "{username}");
$tpl = file_get_contents($tpl_path.$tpl_file);
$new_member_file = str_replace($placeholders, $data, $tpl);
$php_file_name = $username.".php";
$fp = fopen($submissions_path.$php_file_name, "w");
fwrite($fp, $new_submission_file);
fclose($fp); 
?>

模板文件(submit .php)

<html>
<title>{name}</title>
<head>
</head>
<body>
<h1>{name}</h1>
Posted by: {username}
<br/>
<img src="{filename}"/>
</body>
</html>

看起来您可能有路径问题。当您使用路径"/~lyons"时,您可能没有指向您想要的目录。试着做如下修改:

// For use in creating individual page
$tpl_file = "submission.php";
//$tpl_path = "/~lyons/templates/";
$tpl_path = "templates/";

如果有的话,请发布新的错误信息

为了帮助您调试,请尝试打开错误报告和错误显示。

// add after <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

打开文件可能失败,为了更好地控制错误,请尝试:

$fp = @fopen($submissions_path.$php_file_name, "w");
if (!$fp) {
    die('Failed to open file!  Reason: ' . $php_errormsg);
}

我认为你的路径是不正确的,很可能下面两行需要更改为使用完整路径。

// change
$tpl_path = "/~lyons/templates/";
$submissions_path = "/~lyons/submissions";
// to
$tpl_path         = $_SERVER['DOCUMENT_ROOT'] . "/~lyons/templates/";
$submissions_path = $_SERVER['DOCUMENT_ROOT'] . "/~lyons/submissions";

当你去打开文件时,它试图打开/~lyons/templates/,这是一个不存在的目录,它可能是/home/lyons/public_html/templates//home/something/public_html/~lyons/templates/usr/local/apache2/htdocs/~lyons/templates等。$_SERVER['DOCUMENT_ROOT']应该填写正确的值,但在少数情况下,您可能需要手动设置正确的路径,并将其添加到$tpl_path和$submissions_path。

**save the "submission.php" in root folder**
`$tpl_file = "submission.php";`
**create "templates/`" folder in root folder**
`$tpl_path = "templates/";`