Jump to content

[SOLVED] File Deletion


mcmuney

Recommended Posts

This code below is taking an id and deleting the DB row associated. What I'd like to do before this happens is to pull the data in that row and first delete the file, then delete the record. How can that be achieved?

 

$id = addslashes(trim($_GET['id']));

if ($id != "") {
$id2 = $_GET['id'];
$sql2 = mysql_query("DELETE FROM images WHERE id = '$id2'");
}

Link to comment
Share on other sites

that is a very poor query first off because its only verifying that the item is okay to delete, not who initalizied the delete (like is it the right user?) So try something more like

<?php
$id = addslashes(trim($_GET['id']));
$userid = ""; #Supply a user name or some thing else important!
$q = "Select filename as filename, folder as folder, `id` as imgid from `images` where `id` = '".$id."' and OwnerID = '".$userid."'";
$r = mysql_query($q) or die(mysql_error()."<Br /><br />".$q);
if(mysql_num_rows($q) >0){
$row = mysql_fetch_assoc($r);
   $path = $row['folder']."/".$row['filename'];
    if(unlink($path)){
             $q = "Delete from `images` where `id` = '".$row['imgid']."'";
              $r = mysql_query($r) or die(mysql_error()."<br /><br />".$q);
              //we are done
     }
     else{
            //Invalid image path
     }
}
else{
     //Invalid ID and or UserID
}
?>

 

 

That is a much safe version just update the fields (userid, folder, file) and the userid original value.

Link to comment
Share on other sites

Thanks. That solved it. It's not checking for users, because another portion of the code displays delete options based on IP address. This is another issue since it should be driven by cookie and not IP.

 

Your wrong both ways you should drive it by who uploaded the image or who has the permission to delete it, cause you are using get anyone could mark in and theoretically delete an image, I can make your server think my IP is anything I want it to.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.