如何在php中更新图像并从文件夹中删除旧图像并上传新图像

时间:2019-01-10 06:55:05

标签: php mysqli

如何在PHP中更新映像,应该能够在PHP中更新映像,并且   从文件夹中删除旧图像,然后上传新图像。请指导我   怎么做。

这是不删除文件夹中图像的代码

<?php
if ( isset($_POST['btnsave']) ) {

    $sliderName = mysqli_real_escape_string($con, $_POST['sliderName']);

    function GetImageExtension($imagetype)
    {
        if(empty($imagetype)) return false;
        switch($imagetype)
        {
            case 'image/bmp': return '.bmp';
            case 'image/gif': return '.gif';
            case 'image/jpeg': return '.jpg';
            case 'image/png': return '.png';
            default: return false;
        }
    }

    $_FILES["image"]["name"]; 

    $file_name=$_FILES["image"]["name"];
    $temp_name=$_FILES["image"]["tmp_name"];
    $imgtype=$_FILES["image"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y").'-'.time().$ext;
    $target_path = "../ImageSlider/".$imagename;

    if($file_name){
        move_uploaded_file($temp_name, $target_path); 
    }

    $query=" UPDATE `sliderimages` SET tilte='".$sliderName."', 
        image='".$target_path."', userId='".$musid."' WHERE 
        imageId='".$_GET['imageId']."'"; 

    $rest4 = mysqli_query($con,$query);
    //echo"$query";die; 

    if ($query) {
        $errTyp = "success";
        $errMSG = "Successfully registered, ";
        header("refresh:2; addsliderimages.php");

    } else {
        $errTyp = "danger";
        $errMSG = "Something went wrong, try again later..."; 
    } 
}
?>

0 个答案:

没有答案