简体   繁体   中英

Copying uploaded image to the server using PHP

I'm new to PHP and I'm trying to store an image to the database by getting its name property, copying the original file into the server, and insert the name property into the database. Here's my code:

    $image = $_FILES["image"];  
    $img_name = $image["name"];  
    $img_tmp = $image["tmp_name"];
    $dir = "../upload/$img_tmp";


    move_uploaded_file($img_tmp, $dir);

   // Insert into database

I have read a few of questions on adding images through PHP but i still don't get it. It does not copy the file into the server at all, however it stores the name in the database.

Here is the link for Uploading image . I hope it helps you

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

This is the code for uploading image. You just need to get the name of the file and stored it in your database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM