<?php

include "../database.php";

$sql_bacheca    = "SELECT * FROM bacheca ORDER BY id DESC LIMIT 1";
$result_bacheca = mysqli_query($con, $sql_bacheca);
$bacheca        = mysqli_fetch_assoc($result_bacheca);

for ($i = 0; $i < 10; $i++) {
 if (isset($_FILES['file' . $i]['name'])) {
  /* Getting file name */
  $filename = $_FILES['file' . $i]['name'];
  /* Location */
  $location = "../../bacheca_attachments/Pub-" . $bacheca['id'] . "_" . $filename;
  /* Extension */
  $extension = pathinfo($location, PATHINFO_EXTENSION);
  $extension = strtolower($extension);
  /* Allowed file extensions */
  //$allowed_extensions = array("jpg", "jpeg", "png", "pdf", "docx");
  $response = array();
  $status   = 0;
  /* Check file extension */
  //if (in_array(strtolower($extension), $allowed_extensions)) {
  /* Upload file */

  if (move_uploaded_file($_FILES['file' . $i]['tmp_name'], $location)) {

   $status                = 1;
   $response['path']      = $location;
   $response['extension'] = $extension;

   $sql_insert = "INSERT INTO bacheca_attachments VALUES (NULL," . $bacheca['id'] . ",'Pub-" . $bacheca['id'] . "_" . $filename . "','" . $_FILES['file' . $i]['type'] . "','" . $_FILES['file' . $i]['size'] . "')";
   $con->query($sql_insert);
  }

  //}
  $response['status'] = $status;
 }
}

echo json_encode($response);
exit;
echo 0;
