简体   繁体   English

使用PHP将上传的图像复制到服务器

[英]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.我是 PHP 新手,我试图通过获取其 name 属性、将原始文件复制到服务器并将 name 属性插入到数据库中来将图像存储到数据库中。 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.我已经阅读了一些关于通过 PHP 添加图像的问题,但我仍然不明白。 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您只需要获取文件的名称并将其存储在您的数据库中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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