move_uploaded_file在PHP中不起作用

时间:2015-01-26 10:00:35

标签: php

我正在使用PHP进行考试系统。我正在尝试添加图像问题并将图像移动到指定的文件夹以进行检索。我有以下代码:

$sql = 'SELECT q_category, q_image, q_question, q_correct, q_answer2,     q_answer3, q_answer4 FROM tblquestions WHERE q_question = "' . $question . '"';

$retval = mysql_query($sql,$conn);
if(!$retval)
{
    die('Could not get data: ' . mysql_error());
}

while($row1 = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    $cat = $row1['q_category'];
    $quest = $row1['q_question'];
    $image = $row1['q_image'];
    $correct = $row1['q_correct'];
    $ans2 = $row1['q_answer2'];
    $ans3 = $row1['q_answer3'];
    $ans4 = $row1['q_answer4'];

    echo '<tr>';
    echo '<td colspan="2"><div class="selectText" align="center">Edit     Questions</div></td></tr>';
    echo '<tr>';
    echo '<td>Category</td>';
    echo '<td><input type="text" name="txtCategory" value = "' . $cat . '"   size="38"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>Question</td>';
    echo '<td><textarea rows="4" cols="34" name="txtQuestion" id="txtQuestion"  class="addNew">' . $quest . '</textarea>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>Image</td>';
    echo '<td><input type="file" rows="4" cols="28" name="txtImage"  id="txtImage" class="addNew"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>Correct Answer</td>';
    echo '<td><input type="text" name="txtCorrect" value = "' . $correct . '"  size="38"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>Answer 2</td>';
    echo '<td><input type="text" name="txtChoice2" value = "' . $ans2 . '"  size="38"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>Answer 3</td>';
    echo '<td><input type="text" name="txtChoice3" value = "' . $ans3 . '" size="38"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>Answer 4</td>';
    echo '<td><input type="text" name="txtChoice4" value = "' . $ans4 . '" size="38"></td>';
    echo '</tr>';
    echo '<input type="hidden" name="hiddenQuestion" value = "' . $question .  '">';
}

if(isset($_POST['submit1']))
{
    $hiddenQuestion = $_POST['hiddenQuestion'];
    $addCategory = $_POST['txtCategory'];
    $addQuestion = $_POST['txtQuestion'];
    $addCorrect = $_POST['txtCorrect'];
    $addChoice2 = $_POST['txtChoice2'];
    $addChoice3 = $_POST['txtChoice3'];
    $addChoice4 = $_POST['txtChoice4'];

    $filetmp = $_FILES['txtImage']['tmp_name'];
    $filename = $_FILES['txtImage']['name'];
    $filepath = "questionImages/".$filename;

    move_uploaded_file($filetmp,$filepath);

单击“提交”后,$ filename在数据库中为空,move_uploaded_file不起作用。如果我将$ filename = $ _FILES ['txtImage'] ['name']更改为$ filename = $ _POST ['txtImage'],它会将名称添加到数据库中,但仍然无法使用move_uploaded文件。

问题 我在想我的$ filetmp和$ filename有问题。任何人都能指出我的代码中有什么问题吗?在此先感谢:)

1 个答案:

答案 0 :(得分:0)

$filename$filetype似乎没问题。

检查上传表单是否包含enctype="multipart/form-data"

还在问题中包含您的表单代码。