2010-04-16 61 views
1

我想知道如果有人能給我一個例子如何刪除一個圖像使用PHP & MySQL?如何使用PHP和MySQL刪除圖像?

該圖像存儲在一個文件夾名稱thumbs和另一個命名的圖像和圖像名稱存儲在一個mysql數據庫。

+0

可能重複[如何刪除通過PHP文件(http://stackoverflow.com/questions/2371408/如何到刪除-A-文件通過-PHP) – Martin 2012-11-20 20:30:08

回答

8

刪除文件:

unlink("thumbs/imagename"); 

unlink("images/imagename"); 

從數據庫

$sql="DELETE FROM tablename WHERE name='imagename'" 

$result=mysql_query($sql); 

假設name刪除在數據庫中保持所述圖像名稱的字段的名稱,imagename是圖像的名字。

都聚集在代碼:

$imgName='sample.jpg'; 
$dbFieldName='name'; 
$dbTableName='imageTable'; 
unlink("thumbs/$imgName"); 
unlink("images/$imgName"); 
$sql="DELETE FROM $dbTableName WHERE $dbFieldName='$imgName'"; 
mysql_query($sql); 
1

試試這個代碼:

$img_dir = 'image_directory_name/'; 
$img_thmb = 'thumbnail_directory_name/';// if you had thumbnails 

$image_name = $row['image_name'];//assume that this is the image_name field from your database 

//unlink function return bool so you can use it as conditon 
if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){ 
    //assume that variable $image_id is queried from the database where your image record your about to delete is... 
    $sql = "DELETE FROM table WHERE image_id = '".$image_id."'"; 
    $qry = mysql_query($sql); 
}else{ 
    echo 'ERROR: unable to delete image file!'; 
} 
1

您是否在尋找實際的代碼,或只是它背後的想法?

您需要查詢數據庫以找出被刪除文件的名稱,然後使用unlink刪除相關文件。

所以這裏的一些簡單的代碼,讓你開始

<?php 
$thumb_dir = "path/to/thumbs/"; 
$img_dir = "path/to/images/"; 

/* query your db to get the desired image 
    I'm guessing you're using a form to delete the image? 
    if so use something like $image = $_POST['your_variable'] to get the image 
    and query your db */ 

// once you confirm that the file exists in the db check to see if the image 
// is actually on the server 

if(file_exists($thumb_dir . $image . '.jpg')){ 
    if (unlink($thumb_dir . $image . '.jpg') && unlink($img_dir . $image . '.jpg')) 
     //it's better to use the ID rather than the name of the file to delete it from db 
     mysql_query("DELETE FROM table WHERE name='".$image."'") or die(mysql_error()); 
} 
?> 
1
if(!empty($_GET['pid']) && $_GET['act']=="del") 
{ 
    $_sql = "SELECT * FROM mservices WHERE pro_id=".$_GET['pid']; 

    $rs = $_CONN->Execute($_sql); 
    if ($rs->EOF) { 
     $_MSG[] = ""; 
     $error = 1; 
    } 

    if ($rs) 
     $rs->close(); 
    if (!$error) { 

    $_Image_to_delete = "select pro_img from mservices where pro_id=".$_GET['pid']; 
    $trial=$_CONN->Execute($_Image_to_delete); 
    $img = trim(substr($trial,7)); 
    unlink($_DIR['inc']['product_image'].$img); 


     $_sql = "delete from mservices where pro_id=".$_GET['pid']; 
     $_CONN->Execute($_sql); 
     header("Location: ".$_DIR['site']['adminurl']."mservices".$atend."suc".$_DELIM."3".$baratend); 
     exit(); 
    } 
} 
0
$_sql = "SELECT * FROM mservices WHERE pro_id=".$_GET['pid']; 

$rs = $_CONN->Execute($_sql); 
if ($rs->EOF) { 
    $_MSG[] = ""; 
    $error = 1; 
} 

if ($rs) 
    $rs->close(); 
if (!$error) { 

$_Image_to_delete = "select pro_img from mservices where pro_id=".$_GET['pid']; 
$trial=$_CONN->Execute($_Image_to_delete); 
$img = trim(substr($trial,7)); 
unlink($_DIR['inc']['product_image'].$img); 


    $_sql = "delete from mservices where pro_id=".$_GET['pid']; 
    $_CONN->Execute($_sql); 
    header("Location: ".