亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Fix PHP image upload issue: replace previous image to upload new image in multiple posts
P粉788765679
P粉788765679 2023-09-14 11:27:48
0
1
584

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"; // 與服務(wù)器建立連接

    // 文件上傳
    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粉788765679
P粉788765679

reply all(1)
P粉936509635

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template