Question

This code runs when a user hits a delete button on my form. I am trying to copy a file, $picfile, from "/pics/" to "/pics/deletedrecordpics/" and then delete the orginal. Finally, delete the record from the database. Deleting the record from the databse works, but copying the file and deleting the original does nothing. There are no errors in the error log, so I am really confused as to why this code isn't running as I think it should.

if ($allowdelete==true && $thepassword == $password)
{
    //delete record that delete was set to by button

    //$sql = ("DELETE FROM $table WHERE id=$id");
    $sql = ("select picfile,title,author from $table where id=$delete");
    $file=mysql_query($sql);
    $resrow = mysql_fetch_row($file);
    $picfile = $resrow[0];
    $title = $resrow[1];
    $author = $resrow[2];
    if (file_exists("/pics".$picfile)){
        copy("/pics/".$picfile,"/pics/deletedrecordpics/".$author."-".$title."-".$picfile);
        unlink("/pics/".$picfile);
        echo $available = "image is available.";

        $sql = ("DELETE FROM $table WHERE id=$delete");
        $result = mysql_query($sql);

        if ($result){
            echo "Your Picture has been removed from our system.";
            Die($available);
        }
        else{
            echo "There was an error in removing your picture.";
            $Delete = "";
            Die();
        }       
    }
    else{
        echo $available = "image is not available.";
    }
}

The weird part is a have almost the same code in a delete button on my control panel located in "/adminpanel" and it works perfectly. The code for that is the same except I use $id instead $delete and "../" before all the "pics/" because it's in the adminpanel folder. The permissions are right and the folder exists because the code works with that page. And I know $delete is getting set because the record gets deleted from the database. I know picfile, author and title are getting set because I appended them to the print statement and they were all right. Really confused. Any ideas?

Here is the code for the working page

q = ("select picfile,title,author from $table where id=$id");
$file=mysql_query($q);
$resrow = mysql_fetch_row($file);
$picfile = $resrow[0];
$title = $resrow[1];
$author = $resrow[2];
copy("../pics/".$picfile,"../pics/deletedrecordpics/".$author." - ".$title." -     ".$picfile);
unlink("../pics/".$picfile);
$file=mysql_query($q);
$q = ("DELETE FROM $table WHERE id=$id");
$file=mysql_query($q);
Was it helpful?

Solution 2

Try this

$file_path = $_SERVER["DOCUMENT_ROOT"]."/pics/";
if (file_exists($file_path.$picfile))
{
    copy($file_path.$picfile, $file_path."/deletedrecordpics/".$author."-".$title."-".$picfile);
    unlink($file_path.$picfile);
}
else
{
        echo "File not found!!!!!!!!";
}

OTHER TIPS

why is this line repeated twice $file=mysql_query($q); ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top