if (isset($_POST['upload'])) {
  $target = "../img".basename($_FILES['image']['name']);
  $image = $_FILES['image']['name'];
  $msg = "";

  $sql = "UPDATE user SET avatar='$image' WHERE id='$id'";
  mysqli_query($conn, $sql);

  if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
    $msg = "Uploaded file.";
  } else {
    $msg = "there was a problem uploading image";
  }
}





if (isset($_POST['upload'])) {

  $imageName = mysqli_real_escape_string($conn, $_FILES['image']['name']);
  $imageData = mysqli_real_escape_string($conn,file_get_contents($_FILES['image']['tmp_name']));
  $imageType = mysqli_real_escape_string($conn, $_FILES['image']['type']);

  if (substr($imageType, 0, 5) == "image") {
    $sql = "UPDATE user SET avatar='$imageData' WHERE id='$id'";
    mysqli_query($conn, $sql);
    echo "Image uploaded!";

  } else {
    echo "only images are allowed";
  }

}
$sql = "SELECT * FROM user";
  $result = mysqli_query($conn, $sql);
  if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
      $id = $row['id'];
      $sqlImg = "SELECT * FROM profileimg WHERE userid='$id'";
      $resultImg = mysqli_query($conn, $sqlImg);
      while ($rowImg = mysqli_fetch_assoc($resultImg)) {
        echo "<div class='user-container'>";
          if ($rowImg['status'] == 0) {
            echo "<img src='img/profile".$id.".jpg?'".mt_rand().">";
          } else {
            echo "<img src='img/profiledefault.jpg'>";
          }
          echo "<p>".$row['uid']."</p>";
        echo "</div>";
      }
    }
  } else {
    echo "There are no users yet!";
  }

// if ($userRow['image'] == "") {
  echo "<form action='".upload($conn)."' method='POST' enctype='multipart/form-data'>
   <input type='file' name='file'>
   <button type='submit' name='submit'>UPLOAD</button>
   </form>";
  if (isset($_POST['upload'])) {
    } else {
    }
// }

// function upload($conn) {
//   $id = $_SESSION['id'];
//
//
//   if (isset($_POST['submit'])) {
//   	$file = $_FILES['file'];
//   	$fileName = $file['name'];
//   	$fileTmpName = $file['tmp_name'];
//   	$fileSize = $file['size'];
//   	$fileError = $file['error'];
//   	$fileType = $file['type'];
//
//   	$fileExt = explode('.', $fileName);
//   	$fileActualExt = strtolower(end($fileExt));
//
//   	$allowed = array('jpg', 'jpeg', 'png', 'pdf');
//
//   	if (in_array($fileActualExt, $allowed)) {
//   		if ($fileError === 0) {
//   			if ($fileSize < 1000000) {
//   				$fileNameNew = "profile".$id.".".$fileActualExt;
//   				$fileDestination = 'uploads/'.$fileNameNew;
//   				move_uploaded_file($fileTmpName, $fileDestination);
//           $sql = "UPDATE profileimg SET status =0 WHERE userid='$id'";
//           $result = mysqli_query($conn, $sql);
//   				header("Location: index.php?uploadsuccess");
//   			} else {
//   				echo "Your file is too big!";
//   			}
//   		} else {
//   			echo "There was an error uploading your file!";
//   		}
//   	} else {
//   		echo "You cannot upload files of this type!";
//   	}
//   }
// }
