title: How can my upload file script save file path to the database, return an error message, and create a folder?
body: <p>I'm making a script to upload files. I have some questions.</p>
  
  <p>I'm using <a href="https://github.com/enyo/dropzone/" rel="nofollow">the dropzonejs library</a>.</p>
  
  <ol>
  <li><p>I need to save the uploaded file info into the database. I made a prepared statement but it's not working: nothing is stored in the table.</p>
  
  <p>Table info: userID (int 11), image_name (varchar 50), image_path (varchar 60), up_time (varchar 60), up_ip (varchar 45).</p>
  
  <p>Where can I see the MySQL errors? I already checked the .err file from appdata and there was nothing there.</p></li>
  <li><p>How can I return an <code>$error_msg</code> from the upload page to the form page? I'll have to move the first check (file exists) from the upload page to the dropzone.js file but I'm not sure how to do this. Maybe there's  already an option for this, but I searched and found nothing.</p></li>
  </ol>
  
  <h2>HTML:</h2>
  
  <pre><code>&lt;link rel="stylesheet" href="/css/dropzone.css"/&gt;
  
  &lt;script type="text/JavaScript" src="/js/dropzone.js"&gt;&lt;/script&gt;
  
  &lt;form action="/upload"
        class="dropzone"
        id="my-awesome-dropzone"&gt;&lt;/form&gt;
  
  &lt;?php
  if (isset($error_msg)) {
      echo '&lt;p class="error"&gt;' . $error_msg . '&lt;/p&gt;';
  }
  ?&gt;
  </code></pre>
  
  <h2>My upload.php script:</h2>
  
  <pre><code>require_once('/includes/functions.php');
  
  $image = basename($_FILES['file']['name']);
  $image = str_replace(' ','|',$image);
  
  $img_dir = $_SERVER['DOCUMENT_ROOT'] . '/images/user_uploads/' . $_SESSION['username'] . '/';
  
  if (file_exists($img_dir . $image)) {
  
      return $error_msg = $img_dir . $_FILES["file"]["name"] . " ya existe. ";
  
  } elseif (move_uploaded_file($_FILES["file"]["tmp_name"],
      $img_dir . $image)) {
  
      $img_path = $img_dir . $image;
      $up_time = get_current_time();
      $up_ip = get_ip_address();
  
      if ($insert_stmt = $mysqli-&gt;prepare("INSERT INTO user_uploads (userID, image_name, image_path, up_time, up_ip) VALUES (?, ?, ?, ?, ?)")) {
          $insert_stmt-&gt;bind_param('issss', $_SESSION['user_id'], $_FILES["file"]["name"], $img_path, $up_time, $up_ip);
  
          if (! $insert_stmt-&gt;execute()) {
              return $error_msg = '¡Error al subir la imagen! (db_error)';
          }
      }
  } else return $error_msg = '¡Error al subir la imagen!';
  </code></pre>
  
  <p>This works only if I use <code>$_SERVER['DOCUMENT_ROOT']</code> as the image directory.</p>
  
  <ol start="3">
  <li>If the folder does not exist (each user will have their own folder for their uploads), how can I tell the script to automatically create it?</li>
  </ol>
  
  <p>I'm using XAMPP in Windows, but once it's finished I'll upload it to Linux hosting, so maybe I'll have to change the paths or something. So what is the difference between these two operating systems?</p>