将上传的文件移动到服务器并与PHP数据库链接地址时出错

问题描述

我已经创建了一个PHP文件,该文件使用AJAX运行,用于将用户文件上传到服务器并更新其到数据库表的链接。 在运行文件时,我发现我的PHP成功将文件地址与数据库链接在一起,但是该文件未在“上载”文件夹的目标文件夹中移动。不幸的是我找不到错误。您能帮我发现错误吗!我的PHP代码是-

    <?PHP
session_start();
//error_reporting(0);
    $error = 0;
if($_FILES["image"]["name"] !='')
{
    $lastid = $_SESSION['lastid'];
    $img_name = $_FILES["image"]["name"];
    $tempname = $_FILES["image"]["tmp_name"];
    $img_size = $_FILES["image"]["size"];
    
    $img_ext = explode('.',$img_name);
    $ext_check = strtolower(end($img_ext));
    $ext_allwd = array('png','jpg','jpeg');
    $new_name = $lastid ."newname".".".$ext_check;
    $area = "uploads/".$new_name;

    if($ext_check !='jpg' || $ext_check!='jpeg' || $ext_check!='png'){
        $error = 1;
        echo "Only JPG,PNG or JPEG files are allowed.";
    }

    if($img_size > 512000 || $img_size < 51200){
        echo "File size must be 50kb to 500kb";
        $error = 1;
    }
    else{
        $error = 0;
    }

    if($error==0){

        // Create Connection 
        include 'connect.PHP';
        $conn = MysqLi_connect ($host,$dbusername,$dbpassword,$dbname);
        $sql = "UPDATE `admndata` SET `imagename` = '$area' WHERE id = '$lastid'";

        //link with Database
        if (MysqLi_query($conn,$sql)) {

        //move uploaded file
        move_uploaded_file($tempname,$area);

        // close connection after finishing the job
        MysqLi_close($conn);
        }
        else {
        echo "Error updating record: " . MysqLi_error($conn);

        // close connection after an error!
        MysqLi_close($conn);
        }
    }
}
?>

编辑:接受注释建议后编辑的代码

解决方法

嘿,请检查此编码,此编码是否正常工作,它将上载图像并将其保存到我的程序中的“ uploads / uploaddc /”中,并且其工作原理绝对正确。它将检查“ uploads / uploaddc /”文件夹中是否存在相同的命名图像,然后它会告诉用户该图像在该文件夹中,并且如果图像名称不同,则它将毫无问题地上传到文件夹中。我已经与您分享了它,以便您可以检查代码并查找功能缺失的查询

 <?php
    if (isset($_POST['uploaddc'])) {
//adjust as per your folder
        $target_dir = "uploads/uploaddc/";

//check this line with your coding and adjust as per you requirement
        $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);

//check this line with your coding and adjust as per you requirement
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

        $jobid = $_POST['jobid'];
        
        if (move_uploaded_file($_FILES["imageUpload"]["tmp_name"],$target_file)) {
            
        } else {
            echo "Sorry,there was an error uploading your file.";}
        
    
        $checkifsame = "SELECT * FROM images WHERE jobid = '$jobid'";
        $ruencheckquery = mysqli_query($cons,$checkifsame);
        while ($row_category = mysqli_fetch_array($ruencheckquery)) {
            $imagename = $row_category['dcfile_name'];
            global $imagename;
            
        }
    
    
    // this function will tell if the same image is in the database
    
        if($imagename == $target_file){
            echo "Same Image in database . this image cannot be uploaded try other images";
        }else{  
    // adjust as per your database
        $insert_product = "INSERT INTO images(jobid,dcfile_name)  VALUES ('$jobid','$target_file')";
        $run_query = mysqli_query($cons,$insert_product);
    }
    
    }
    ?>  

最后特别检查您<form action="" method="POST" enctype="multipart/form-data">,因为最近我添加了put。或其他错误的词将查询发送到数据库,但没有将图片移动到特定的文件夹。因此,请同时检查此部分。

如果能以其他方式解决问题,我将非常高兴,我会尽力为您提供帮助