PHP在我的根文件夹中的数据库和文件夹中上传

So I'm trying to upload the file name of the image in the database and the image itself will be save in the folder inside a root folder.

In my codes, the image name is able to be saved in the database but the image itself did not save in the folder. Why is that?

Here's my code:

<?php

include '../session.php';
require_once 'config.php';


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

    $img_dir = "updated_photo/";
    $target_file = $img_dir . basename($_FILES["image"]["name"]);
    $imageName = $_FILES["image"]["name"];
    $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
    $imageType = $_FILES["image"]["type"];

        if (substr($imageType, 0,5) == "image") {
            $query = "UPDATE `crew_info` SET `image_name` = ? WHERE `id` = ?";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, 'si', $imageName, $_POST['id']);
            mysqli_stmt_execute($stmt);
            $id = $_POST['id'];

            header("Location: ../admin/view_all_info.php?id=$id");

        }

        else {

            echo "Image not Uploaded!";

        }

}

?>

What seems to be the problem of my codes?

Here try this, I edited your codes.

<?php

include '../session.php';
require_once 'config.php';


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

    $img_dir = "../updated_photo/";
    $target_file = $img_dir . basename($_FILES["image"]["name"]);
    $imageName = $_FILES["image"]["name"];
    $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
    $imageType = $_FILES["image"]["type"];

        if (substr($imageType, 0,5) == "image") {
            $query = "UPDATE `crew_info` SET `image_name` = ? WHERE `id` = ?";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, 'si', $imageName, $_POST['id']);
            mysqli_stmt_execute($stmt);
            file_put_contents($target_file, $imageData);
            $id = $_POST['id'];
            header("Location: ../admin/view_all_info.php?id=$id");

        }

        else {

            echo "Image not Uploaded!";

        }

}

?>

just copy and paste it. tell if it worked.

You just get the contain of file. forget to put in target folder use file_put_contents

 $target_file = $img_dir . basename($_FILES["image"]["name"]);
 $imageName = $_FILES["image"]["name"];
 $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
// Write the contents back to the file
file_put_contents($target_file, $imageData);

You can also use move_uploaded_file to uploaded file to a new location

<?php

include '../session.php';
require_once 'config.php';


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

    $img_dir = "updated_photo/";
    $target_file = $img_dir . basename($_FILES["image"]["name"]);
    $imageName = $_FILES["image"]["name"];
    $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
    $imageType = $_FILES["image"]["type"];

        if (substr($imageType, 0,5) == "image") {
            $query = "UPDATE `crew_info` SET `image_name` = ? WHERE `id` = ?";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, 'si', $imageName, $_POST['id']);
            mysqli_stmt_execute($stmt);
            $id = $_POST['id'];
            //remove
     if (file_exists($target_file))
    {
        $result = unlink($target_file);
        if (!$result)
        {
            //error, display error, 
            return false;
        }
    }
            //upload
             $result  =  move_uploaded_file($_FILES['image']['tmp_name'], $img_dir);

                if(!$result) 
                    {
                    //error upload
                     }

            header("Location: ../admin/view_all_info.php?id=$id");

        }

        else {

            echo "Image not Uploaded!";

        }

}

?>

try