I am trying to upload images to a MySQL database using the LAMP stack.
HTML form:
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>File: </label>
<input type="file" name="image">
<input type="submit" value="Submit" name="submit">
</form>
PHP script:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST["submit"])) {
echo $_POST["submit"];
var_dump($_FILES);
}
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "users";
$conn = new mysqli($servername, $username, $password, $dbname);
session_start();
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$sql = "INSERT INTO `userprofile` (`id`, `image`, `image_name`) VALUES ('1', '{$image}', '{$image_name}')";
if (!$conn->query($sql)) { // Error handling
echo "Something went wrong! :(";
}
session_destroy();
?>
I got the following warning:
Undefined index: image in /upload.php
I have tried everything in the following post: https://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php#=
This includes setting permissions on upload_tmp_dir
and making sure file_uploads
is on.