如何使用jQuery / Ajax / PHP删除上传的图像?

时间:2016-05-20 05:43:43

标签: php jquery ajax

我正在使用以下脚本上传图片。这是链接:http://filer.grandesign.md/

使用此脚本上传图像后允许预览。像贝娄图像:

enter image description here

您可以看到,它还允许删除图像 - 查看红色桶图标

我现在在做什么:

当我上传图片时,我重命名了上传的图片并将其保存到数据库。

代码如下:

require_once('class.upload.php');
if(!isset($_FILES['files'])) {
    die();
}

$files = array();
foreach ($_FILES['files'] as $k => $l) {
   foreach ($l as $i => $v) {
       if (!array_key_exists($i, $files))
           $files[$i] = array();
       $files[$i][$k] = $v;
   }
}

foreach ($files as $file) {

    $handle = new upload($file);

    if ($handle->uploaded) {

        $handle->file_new_name_body     = 'mpic_list_'.uniqid('', true);
        $menu_list_image = $handle->file_new_name_body;

        $handle->image_resize          = true;
        $handle->image_ratio_crop      = true;
        $handle->image_x               = 360;
        $handle->image_y               = 240;   

        $handle->process('images/menu_images/');    

        $handle->file_new_name_body     = 'mpic_small_'.uniqid('', true);
        $menu_small_image = $handle->file_new_name_body;

        $handle->image_resize          = true;
        $handle->image_ratio_crop      = true;  
        $handle->image_x               = 100;
        $handle->image_y               = 65;

        $handle->process('images/menu_images/');

        $handle->file_new_name_body     = 'mpic_large_'.uniqid('', true);
        $menu_large_image = $handle->file_new_name_body;

        $handle->image_resize          = true;
        $handle->image_ratio_crop      = true;  
        $handle->image_x               = 700;
        $handle->image_y               = 470;

        $handle->process('images/menu_images/');

        if ($handle->processed) {           
            $all_images = $menu_list_image . $menu_small_image . $menu_large_image;
            $u_id = (int) $_SESSION['logged_user_id'];

            if(!isset($_SESSION['last_id'])) {
                // insert upload image section data...
                $insert_menu_details = mysqli_query($conn, "INSERT INTO products (p_id) VALUES ('')");
                $last_id = mysqli_insert_id($conn);
                $insert_upload_image = mysqli_query($conn, "INSERT INTO product_images VALUES ('', '$menu_large_image', '$menu_list_image', '$menu_small_image', '$last_id', '$u_id')");    
                $_SESSION['last_id'] = $last_id;
            } else {
                // update upload image section data
                $session_last_id = $_SESSION['last_id'];
                $update_upload_image = mysqli_query($conn, "INSERT INTO product_images VALUES ('', '$menu_large_image', '$menu_list_image', '$menu_small_image', '$session_last_id', '$u_id')");    
            }

            $handle->clean();
        } else {
            //echo 'error : ' . $handle->error;
            echo 'Error';
        }
    }   
}

我需要什么:

现在我要删除上传的图片。但是这里存在一个问题:默认情况下,此脚本使用以下PHP行删除上载的图像:

<?php 
if(isset($_POST['file'])){
    $file = 'images/menu_images/' . $_POST['file'];
    if(file_exists($file)){
        unlink($file);
    }
}
?>

但是我无法将其删除,因为当我将图片上传到文件夹(images/menu_images/)时,我将其重命名为:abedkd12415775554.jpg

我的问题如何使用此脚本删除上传的图片?

2 个答案:

答案 0 :(得分:0)

您需要返回从服务器脚本中生成的新图像名称,如

在循环中执行以在数据库中插入文件名 -

$array = array("oldName" => "newName");
echo json_encode($array);

如果您在javascript端使用某些逻辑来创建数组,也可以使用数字索引。

在javascript on delete选项中,您可以使用图像名称检索值,并可以执行删除。

答案 1 :(得分:0)

检查这个

is blue present
  

替换您的表和ID名称