0

I've read about 15 threads on here, tried each and none have worked yet, the issue seems to be around uploading a picture using blob (yes I know that I shouldn't and as it scales it will cause a problem but this is only a prototype). Seem that even though I am passing in the file, $_FILES['picture'] is not set. If i remove the if and get to something like $imageName = $_FILES['picture']['name']; it says the index pictures does not exist. Any help would be appreciated greatly, thanks.

Here is my html code:

<!DOCTYPE html>
<html>
<head>
<?php include ('header.php'); ?>
<title>Add item</title>
</head>
<body>

<h1>Add item:</h1>

<form method="post" action="additem.php">
Item Name:
<input type="varchar" name="name" maxlength=64>
<br><br>

Description:<br>
<textarea name="description">
</textarea>
<br><br>

Date added:
<input type="date" name="date">
<br><br>

Picture:
<input type="file" name="picture" value="picture">
<br><br>

Shop:
<input type="varchar" name="shop" maxlength=64>
<br><br>

Type:
<input type="varchar" name="type" maxlength=15>
<br><br>

subtype:
<input type="varchar" name="subtype" maxlength=32>
<br><br>

<input type="submit" enctype="multipart/form-data" method="post" name="submit" value="Add event">
</form>
<br><br>

and my php code:

<?php

if (empty($_SESSION['username'])) {
    echo "Not logged in";
    exit;
}  else {
    if ($_SESSION['privilege'] == "student") {
        echo $_SESSION['username'];
        echo " does not have permission to create events ";
        exit;
    } else {
        echo $_SESSION['username'];
        echo " creating new item";
    }
}

if(!empty($_POST)) {
    require_once('connectdb.php');

    $name = $_POST['name'];
    if(empty($name)) {
        echo(" You must enter an item name.");
        exit;
    }

    $date = $_POST['date'];
    if(empty($date)) {
        echo(" You must enter an item add date.");
        exit;
    }


    $shop= $_POST['shop'];
    if(empty($venue)) {
        echo(" You must enter an shop.");
        exit;
    }

    if (isset($_FILES['picture'])){
    $imageName = $_FILES['picture']['name'];
    $imageData = $_FILES['picture']['tmp_name'];
    $imageType = $_FILES['picture']['type'];

    if(substr($imageType,0,5) == "image")
    {
        echo "this is an image";
    } else {
        echo "Incorrect file type";
        exit;
    }

    $description = $_POST['description'];
    $type = $_POST['type'];
    $subtype = $_POST['subtype'];
    $organiser = $_SESSION['username'];


    try {
        $stmt = $db->prepare("INSERT INTO `items` (`name`, `description`, `date`, `picturename`, `picture`, `organiser`, `shop`, `type`, `subtype`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute(array($name, $description, $date, $imageName, $imageData, $_SESSION['username'], $shop, $type, $subtype));
        echo("Successful.");
    } catch(PDOException $ex) {
        echo("Failed to save data to database.<br>");
        echo($ex->getMessage());
        exit;
    }
    }else{
        echo "file not set";
    }

}

?>
</body>
</html>

1 Answer 1

2

You should specify a non-default encoding for your form - otherwise the browser will not upload your file at all:

<form method="post" action="additem.php" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.