I am trying to make a script in PHP which is a basic file upload function. Everything is done and everything works except the move_uploaded_file command. So what my program does in brief: You can upload an image from your PC and it renames it and uploads it to my localhost /uploads/ folder with a new name which is the current time. The problem is that, the program is not moves the file into the /uploads/ folder, but rename it, you can see it in the MYSQL Database, because the new path and name are correct. I am struggling with this one bug or error or I don't really know what it is. (Before the renaming function everything was working fine, you could upload your file into the /uploads/ folder). Any idea what should I do?
Code:
<?php
$con = mysqli_connect("localhost", "root", "", "dotech");
$target_dir = '/uploads/';
$target_file = $target_dir . basename($_FILES['fileToUpload']['name']);
$target_type = basename($_FILES['fileToUpload']['type']);
$target_ext = strtolower(end(explode('.',$_FILES['fileToUpload']['name'])));
$target_tmp = $_FILES['fileToUpload']['tmp_name'];
$target_size = $_FILES["fileToUpload"]["size"];
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(!$con) {
  die("Connection failed: " . mysqli_connect_error());
}
$user_id = mysqli_real_escape_string($con, $_POST['cpmsbtndbldsrd']);
$post_text = mysqli_real_escape_string($con, $_POST['user_post_textarea']);
$post_tag = mysqli_real_escape_string($con, $_POST['tag_select']);
if(isset($_POST["create_post"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
      $uploadOk = 1;
    } else {
      $uploadOk = 0;
      header("Location: /p/index.php?id=$user_id&fileupload=invalid_file_type");
      exit();
    }
    if ($uploadOk = 1) {
        $target_file = $target_dir.''.date('YmdHis').'.'.$target_ext;
        $post_file = $target_file;
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
        if ($stmt = $con->prepare('INSERT INTO user_posts (user_id, post_desc, post_file, post_file_type, post_file_size, post_tags) VALUES (?, ?, ?, ?, ?, ?)')) {
            $stmt->bind_param('isssis', $user_id, $post_text, $post_file, $target_type, $target_size, $post_tag);
            $stmt->execute();
            header("Location: /p/index.php?id=$user_id&fileupload=success");
            exit();
        } else {
            echo 'Could not prepare statement!';
        }
        $stmt->close();
    }
  }
?>
EDIT:
So I made changes on my code and I have a new error or I don't even know what to call this. By the way, it finally move the renamed file into the /uploads/ folder, but not insert into the database.
CHANGES:
$tmp = explode('.', $target_name);  // New exploding method
$target_ext = @end($tmp);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
    $stmt = $con->prepare('INSERT INTO user_posts (user_id, post_desc, post_file, post_file_type, post_file_size, post_tags) VALUES (?, ?, ?, ?, ?, ?)');
        $stmt->bind_param('isssis', $user_id, $post_text, $post_file, $target_type, $target_size, $post_tag);
        $stmt->execute();
        header("Location: /p/index.php?id=$user_id&fileupload=success");
        exit();
} else {
    var_dump($_FILES["fileToUpload"]);
}

 
    