0

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.

Community
  • 1
  • 1
Rajan
  • 43
  • 7
  • Try checking `$_FILES['image']['error']` and also check out [the manual for uploading files in PHP](http://php.net/manual/en/features.file-upload.php) – RiggsFolly May 21 '17 at 17:54
  • Thanks @RiggsFolly. I incorporated the error checking code. It seems that $_FILES does not exist. When I click submit, the $_FILES variable doesn't contain any data. – Rajan May 21 '17 at 19:16
  • I was using JQuery Mobile. You have to set data-ajax="false" http://demos.jquerymobile.com/1.4.3/forms/#FileInputsAjax – Rajan May 22 '17 at 07:05

0 Answers0