简体   繁体   English

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

[英]How to delete uploaded image using jQuery / Ajax / PHP?

I am using following script to upload images. 我正在使用以下脚本上传图像。 Here is the link : http://filer.grandesign.md/ 这是链接: http : //filer.grandesign.md/

Using this script It's allowing the preview after upload the image. 使用此脚本允许在上传图像后进行预览。 Like bellow image : 像波纹管图像一样:

在此处输入图片说明

You can see that, it's also allowing to delete the Image - See red bucket icon 您可以看到,它还允许删除图像-请参见红色存储桶图标

What I am doing now : 我现在在做什么:

When I upload the image I renamed the uploaded image and save it to database. 当我上传图像时,我重命名了上传的图像并将其保存到数据库。

The code is bellow : 代码如下:

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';
        }
    }   
}

What I need : 我需要的 :

Now I want to delete my uploaded image. 现在,我要删除上传的图像。 But here is an issue which is : by default this script is deleting the uploaded image using following PHP line : 但这是一个问题:默认情况下,此脚本使用以下PHP行删除上传的图像:

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

But I can't delete it because when I upload the image to folder ( images/menu_images/ ) I renamed it to something like that : abedkd12415775554.jpg 但是我无法删除它,因为当我将图像上传到文件夹( images/menu_images/ )时,我将其重命名为类似的名称: abedkd12415775554.jpg

My Question is How can I delete my uploaded image using this script ? 我的问题是如何使用此脚本删除上传的图像?

You need to return the new images name that you are generating from server scripting like- 您需要返回从服务器脚本生成的新图像名称,例如-

In the loop you are executing for inserting filename in database- 在循环中,您正在执行在数据库中插入文件名的操作,

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

You can also use numeric index if you are using some logic at your javascript end for creating array. 如果您在JavaScript端使用某种逻辑来创建数组,也可以使用数字索引。

In javascript on delete option you can retrieve the value by using the image name and can perform delete. 在javascript on delete选项中,您可以使用图像名称检索值,并可以执行删除操作。

check this 检查这个

$res=mysqli_query("SELECT file FROM tbl_uploads WHERE id=".$_GET['remove_id']);
$row=mysqli_fetch_array($res);
mysqli_query("DELETE FROM tbl_uploads WHERE id=".$_GET['remove_id']);
unlink("uploads/".$row['file']);

Replace Your table and id name 替换您的表格和ID名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM