2016-05-20 141 views
0

我正在使用以下腳本上傳圖像。這裏是鏈接:http://filer.grandesign.md/如何使用jQuery/Ajax/PHP刪除上傳的圖片?

使用此腳本它允許上傳圖像後預覽。像波紋管圖像:

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

我的問題是如何使用此腳本刪除上傳的圖像?

回答

0

您需要返回您是從服務器腳本生成新的圖像名喜歡 -

在循環您正在執行對數據庫 -

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

您還可以使用數字索引插入文件名如果你在javascript端使用一些邏輯來創建數組。

在JavaScript上刪除選項,您可以通過使用圖像名稱檢索值,並可以執行刪除。

0

檢查這個

$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']); 

更換你的表與身份證姓名