Bazı biri PHP & kullanarak bir görüntüyü silmek için nasıl bana bir örnek verebilir merak ediyordum MySQL?
Görüntü bir klasör adı başparmak ve başka bir adli görüntüleri içinde saklanır ve görüntü adı mysql veritabanında saklanır.
Delete the file:
unlink("thumbs/imagename");
unlink("images/imagename");
Remove from database
$sql="DELETE FROM tablename WHERE name='imagename'"
$result=mysql_query($sql);
name görüntü adını tutan veritabanındaki alanın adıdır varsayarsak, ve imagename görüntünün adıdır.
Kod birlikte tüm:
$imgName='sample.jpg';
$dbFieldName='name';
$dbTableName='imageTable';
unlink("thumbs/$imgName");
unlink("images/$imgName");
$sql="DELETE FROM $dbTableName WHERE $dbFieldName='$imgName'";
mysql_query($sql);
Bu kodu deneyin:
$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!';
}
Bunun arkasında gerçek kodu ya da sadece fikir mi arıyorsunuz?
Sen silinen dosya adını bulmak ve sonra sadece söz konusu dosyayı silmek için unlink kullanmak için db sorgulamak gerekir.
bu nedenle burada Başlamak için bazı hızlı kod
<?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());
}
?>