PHP未定义索引错误和带有$ _Files的未定义变量

时间:2018-05-14 05:24:45

标签: php html mysql

我正在尝试创建一个将脚本上传到文件夹/ img的PHP脚本,并将图片目录保存为数据库$ img_location。当我尝试上传图片时,我收到了这些错误。

    Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 65
    Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 67
    Notice: Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 68
    Notice: Undefined index: select_picture in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 69
    Notice: Undefined variable: img_location in /Applications/XAMPP/xamppfiles/htdocs/ia2/post.php on line 115

这是PHP处理图片输入

elseif (!$tid && !empty($_POST['select_picture'])) {
    $name = $_FILES['select_picture']['name']; //line 65
    var_dump($_FILES);
    $size = $_FILES['select_picture']['size']; //line 67
    $type = $_FILES['select_picture']['type']; //line 68
    $tmp_name = $_FILES['select_picture']['tmp_name']; //line 69
    $max_size = 20000000;
    $extension = substr($name, strpos($name, '.') + 1);

    if (isset($name) && !empty($name)) {
        if (($extension == "jpg" || $extension == "jpeg") && $type == "image/jpeg" && $extension == $size <= $max_size) {
            $location = "img/";
            if (move_uploaded_file($tmp_name, $location . $name)) {
                $img_location = "img/$name";
                $smsg = "Uploaded Successfully";
            } else {
                $fmsg = "Failed to Upload File";
            }
        } else {
            $fmsg = "File size should be 20 MegaBytes & Only JPEG File";
        }
    } else {
        $fmsg = "Please Select a File";
    }

以下是将图像位置与其他信息一起插入数据库的部分

        if ($tid) { // Add this to the replies table:
        $q = "INSERT INTO posts (thread_id, user_id, img_location, description_title, description, posted_on) VALUES ($tid, {$_SESSION['user_id']}, '" . mysqli_real_escape_string($dbc, $img_location) . "', '" . mysqli_real_escape_string($dbc, $description_title) . "', '" . mysqli_real_escape_string($dbc, $description) . "',UTC_TIMESTAMP())"; //this is line 115
        $r = mysqli_query($dbc, $q);
        if (mysqli_affected_rows($dbc) == 1) {
            echo '<p>Your post has been entered.</p>';
        } else {
            echo '<p>Your post could not be handled due to a system error.</p>';
        }
    } // Valid $tid.

这是前端

// Only display this form if the user is logged in:
if (isset($_SESSION['user_id'])) {

// Display the form:
echo '<form action="post.php" method="post" accept-charset="utf-8" enctype="multipart/form-data>';
//lines between removed
    // Create picture input:
    echo '<div class="form-group">';
    if (isset($smsg)) {echo '<div class="alert alert-success" role="alert"';
        echo $smsg;
        echo '</div> }';}
    if (isset($fmsg)) {echo '<div class="alert alert-danger" role="alert">';
        echo $fmsg;
        echo '</div> }';}
    echo '<label for="select_picture">' . $words['select_picture'] . '</label>
    <input type="file" class="form-control" name="select_picture" id="select_picture" ';

    // Check for existing value:
    if (isset($select_picture)) {
        echo "value=\"$select_picture\" ";
    }

    echo '/><p class="help-block">Upload JPEG Files that are below 20 MB</p></div>';
} // End of $tid IF.

2 个答案:

答案 0 :(得分:1)

$ _ POST和$ _FILES是2个不同的数组。 $ _FILES存储有关您上传文件的信息,$ _POST以表格形式存储所有其他字段(例如<input name="name" />)。 在这种情况下,您应该检查$ _FILES是否为空。

答案 1 :(得分:0)

您在$ _POST中有检查条件,但图片会进入$ _FILES,我认为它会解决您的错误

elseif (!$tid && !empty($_FILES['select_picture'])) {
$name = $_FILES['select_picture']['name']; //line 65
var_dump($_FILES);
$size = $_FILES['select_picture']['size']; //line 67
$type = $_FILES['select_picture']['type']; //line 68
$tmp_name = $_FILES['select_picture']['tmp_name']; //line 69
$max_size = 20000000;
$extension = substr($name, strpos($name, '.') + 1);

if (isset($name) && !empty($name)) {
    if (($extension == "jpg" || $extension == "jpeg") && $type == "image/jpeg" && $extension == $size <= $max_size) {
        $location = "img/";
        if (move_uploaded_file($tmp_name, $location . $name)) {
            $img_location = "img/$name";
            $smsg = "Uploaded Successfully";
        } else {
            $fmsg = "Failed to Upload File";
        }
    } else {
        $fmsg = "File size should be 20 MegaBytes & Only JPEG File";
    }
} else {
    $fmsg = "Please Select a File";
}

当你检查条件时,每件事情看起来都不错,但只需将$_POST更改为$_FILES