PHP文件上传无法正常工作

时间:2012-05-31 15:21:12

标签: php mysql file upload

我正在尝试更新数据库中的记录。该代码旨在允许用户更新网站上的条目,同时也可以选择编辑图像。当我最初测试此代码时,它没有任何问题。当他们选择图像时,它会更新图像,当他们没有选择图像时,它不会在更新中包含图像。当我将此代码移动到它需要的页面时,它不再有效。它总是在阅读它,好像用户没有选择要上传的图像。测试代码与此代码之间唯一的变化是数据库中的名称,以及为变量$ title和$ description添加mysql_real_escape_string()。

以下是不适用于我的PHP代码:

<?php
require_once ("connect.php");
if (isset($_POST['description'])) {
    $id = $_GET['id'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    $title = mysql_real_escape_string($title);
    $description = mysql_real_escape_string($description);
    $target = "../images/contests/";
    $target = $target.basename( $_FILES['image']['name']);
    $ok=1;

    if($_FILES['image']['name'] == "") {
        $query = "UPDATE tbl_contests SET contests_title='$title', contests_description='$description' WHERE contests_id='$id'";
        $result = mysql_query ($query);
            if ($result) {
            header ("Location: contests.php?=noimage");
            exit ();
        } else {
            header ("Location: contests.php?=error");
            exit ();
        }
    } else {
        if ($ok==0){
            header("Location: contests.php?=error");
        } else {
            if(move_uploaded_file($_FILES['image']['tmp_name'], $target)){
                echo "<p>Your upload was sucessful.</p>";
                $query = "UPDATE tbl_contests SET contests_title='$title', contests_description='$description', contests_image='$target' WHERE contests_id='$id'";
                $result = mysql_query ($query);
                if ($result) {
                    header ("Location: contests.php?=image");
                    exit ();
                } else {
                    header ("Location: contests.php?=error");
                    exit ();
                }
            }
        }
    } 
}
?>

以下是与上述代码相关的表格:

<?php
    $postnum = $_GET['id'];
    $query = "SELECT * FROM tbl_contests WHERE contests_id=".$postnum;
    $result= mysql_query($query);
    $row = mysql_fetch_array($result);
    $path = "../images/contests/";
    ?>

    <form action="update-past.php?id=<?php print $row[contests_id]; ?>" method="post" id="updatepast">
    <br /><label>Title:</label> <p><input type="text" name="title" id="title" class="input" value="<?php print $row[contests_title]; ?>" /></p>
    <?php if ($row['contests_image'] == !null) { ?>
    <p><img src="<?php print $path.$row['contests_image']; ?>" width="425" height="500" /></p>
    <br /><label>Edit Image: (Optional)</label> <p><input name="image" type="file" id="image" class="file" size="50" /></p>
    <?php } else { ?>
    <br /><br /><br /><br /><label>Add Image: (Optional)</label> <p><input name="image" type="file" id="image" class="file" size="50" /></p>
    <?php } ?>
    <br /><br /><br /><br /><br /><label>Description:</label><p><textarea name="description" cols="85" id="description" class="contentinput" rows="10"><?php print $row[contests_description]; ?></textarea></p>
    <p><input type="submit" name="submit" id="button" value="Edit" /></p>
    </form>

1 个答案:

答案 0 :(得分:1)

尝试将其添加到表单中:enctype="multipart/form-data"

以下是对表单内容类型的一些解读:http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.2