1

I'm trying to create a PHP script that will upload picture to a folder /img, and save picture directory to database as $img_location. When I try to upload a picture I get these errors.

    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

Here is the PHP handling picture input

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";
    }

Here is the part where image location is inserted into the database along with other information

        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.

Here is the frontend

// 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.
Jelly C
  • 11
  • 2

2 Answers2

1

$_POST and $_FILES are 2 different arrays. $_FILES stores info about your uploaded file(s), $_POST stores all other fields in form (e.g <input name="name" />). In this case you should check if $_FILES empty.

0

You have check condition in $_POST but image will get into $_FILES, i think it will resolve your error

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";
}

Every thing is look like good but just change $_POST into $_FILES when you check condition.

nageen nayak
  • 1,262
  • 2
  • 18
  • 28
  • I followed your instruction and changed $_POST to $_FILES, but it shows "Please Select a File" somehow and still have error "Undefined variable: img_location" in line 115. – Jelly C May 14 '18 at 07:23