search

Home  >  Q&A  >  body text

Fix PHP image upload issue: replace previous image to upload new image in multiple posts

I have an issue with the image upload functionality of my PHP application. details as following:

I have a form where the user can create a post and upload an image related to the post. When I upload my first post with an image, everything works fine and the image is saved correctly to the server in a folder called Uploads and saved under a name (for example, post1_image. jpg). However, the problem arises when I try to upload a second post with a different image with the same name as the first image (for example, post1_image.jpg). Instead of saving it as a new image, it replaces the previous image associated with the first post. How can I modify my PHP code to ensure that each uploaded image has a unique name and does not overwrite any existing images? Here is a simplified version of the PHP code I currently use to handle image uploads:

<?php 
    include "config.php"; // 与服务器建立连接

    // 文件上传
    if (isset($_FILES['fileToUpload'])) {
        $Errors = array();

        $File_Name = $_FILES['fileToUpload']['name'];
        $File_Size= $_FILES['fileToUpload']['size'];
        $File_Tmp = $_FILES['fileToUpload']['tmp_name'];
        $File_Type = $_FILES['fileToUpload']['type'];
        $File_ext = end(explode('.',$File_Name));
        $Extensions = array('jpeg','jpg','png');
        if (in_array($File_ext,$Extensions) === false) {
            $Errors[] = "不允许上传此文件。您只能上传jpeg,jpg和png文件";
        }
        if ($File_Size > 2097152) {
            $Errors[] = "您不能上传大于2MB的文件";
        }
        
        if (empty($Errors) == true) {
            
            move_uploaded_file($File_Tmp,"upload/". $File_Name);
        }else{
            print_r($Errors);
            die();
        }
    }

    session_start();
    $Title = mysqli_real_escape_string($conn,$_POST["post_title"]);
    $Description = mysqli_real_escape_string($conn,$_POST["postdesc"]);
    $Category = mysqli_real_escape_string($conn,$_POST["category"]);
    $date = date("D M,Y");
    $Auther = $_SESSION['name'];

    

    $sql = "insert into post (title,description,category,post_date,author,post_img) values ('{$Title}', '{$Description}', {$Category}, '{$date}', '{$Auther}','{$File_Name}');";
    $sql .= "Update category set post = post+1 where category_id = $Category;";
    // $result =  mysqli_multi_query($conn, $sql) or die ("Query unsucceful"); 
    
    if (mysqli_multi_query($conn, $sql)) {
        mysqli_close($conn);
        
        header('Location: http://'.$hostname.'/cms/admin/post.php');
    }else {
        echo "查询失败";
    }
    
?>

Thank you very much for any insights or suggestions on how to solve this problem! Thanks!

P粉788765679P粉788765679438 days ago464

reply all(1)I'll reply

  • P粉936509635

    P粉9365096352023-09-15 00:33:10

    You can use the uniqid() function to ensure that each uploaded image has a unique name.

    reply
    0
  • Cancelreply