-2

I am trying to delete images when the user click on delete

if (isset($_GET['sid'])) {
    $sid = $_GET['sid'];
}


$getImageName = "SELECT * FROM header_image_arabic WHERE id='" . $sid . "'";
$QgetImageName = $db->query($getImageName) or die($db->error);
if ($Fname = $QgetImageName->fetch_object())
    $myImageName = $Fname->image;
// delete image from dir function
$dir = "../images/backSlider_arabic/";
opendir($dir);
if (dir($dir)) {
    $filename = $myImageName;
    if (file_exists($filename)) {
        unlink("../images/backSlider_arabic/" . $filename);
        echo 'File' . $filename . 'has been deleted';
    } else {
        echo 'Could not delete ' . $filename . ',file does not exist';
        echo "<br />" . dirname("../images/backSlider_arabic/") . ".<br />";
    }
} else {
    echo "Dir not there";
}
closedir();

every time I click on delete it come with "Could not delete myfile.jpg, file does not exist'"

Edit mySql with more security

if(isset($_GET['sid'])){
     $sid=$_GET['sid'];
}
    $getName = $db->prepare("SELECT * FROM header_image_arabic WHERE id=?");
    $getName->bind_param('s', $sid);
    $getName->execute();
    $result = $getName->get_result();
    if($Fname=$result->fetch_object())
    $myImageName=$Fname->image;
    //delete image from dir function
    $dir="../images/backSlider_arabic/";
    opendir($dir);
    if(dir($dir)){
    $filename=$myImageName;
    if(file_exists("../images/backSlider_arabic/".$filename)) {
    unlink("../images/backSlider_arabic/".$filename);
    echo'File'.$filename.'has been deleted';
    }else{
    echo 'Could not delete '.$filename.',file does not exist';
    echo "<br />".dirname("../images/backSlider_arabic/").".<br />";
    }
    }else{echo"Dir not there";}
    closedir();

I have updated my code since the mySql query was very poor on security I used Prepared statements to better way.

Mikky
  • 101
  • 2
  • 12

2 Answers2

1

First of all, you really need to properly check and escape your variables! Your script is really bad thought about.

However, if it is for learning, regarding your question, an error is problably in:

if(file_exists($filename)) {

which should be

if (file_exists("../images/backSlider_arabic/".$filename)) {

But please, adjust and fix your script, you are leaving it open for anyone like this, to delete whatever they like.

dkasipovic
  • 5,930
  • 1
  • 19
  • 25
  • 1
    i thought of that too ... but then i saw `opendir($dir);` do you need the full path when navigating into the folder before? – roeb Feb 25 '14 at 13:32
  • 2
    I dont think so, but this serves more as an educational answer. I was trying to say that he should check for and delete same file. In his script he is checking one file, and deleting another. Meaning, if he trully does not need the path in file_exists() then the unlink() file path is wrong. Or vice versa, if he needs the path the file_exists() is wrong. That's why I want to point out that he should check for and delete the same file. – dkasipovic Feb 25 '14 at 13:35
  • @kasipovic: you are totally right about that – roeb Feb 25 '14 at 13:39
0

i hope you check for more things than isset($_GET['sid'])

for debugging purpose use echo or var_dump to show the path/image before you try to delete it

and check your server if you have the right rights (read/write)

roeb
  • 467
  • 8
  • 19