Unlink() help

 
0
 

Hi could someone help me out with this code please?
I have commented what it should do(or ewhat I want it to do!)

if(isset($_POST['remove'])){
      $chk = (array) $_POST['remove'];
      $p = implode(',',array_keys($chk)); 
      $q = mysql_query("DELETE FROM image_gallery WHERE id IN ($p)");
      $t = mysql_query("SELECT filename from image_gallery WHERE id IN ($p)"); //filename is the path of the file.. eg. image/siteimages/logo.gif
      $s = mysql_query("DELETE FROM thumbs_gallery WHERE id IN ($p)");

      if ($sql = $s){
        $q;
        unlink($t); //this isnt doing anything at all
        header( 'Location: lee_remove.php' ) ;
      }

The entries get deleted from both tables ok, but the image file does not get deleted from the dir.
Can anyone point out where im going wrong??

Thanks for looking...............

Featured Replies in this Discussion

  • $sql doesn't exist. $s returns boolean (success/failure). What is the point of line 9? If all the images are in the same folder, you can dispense with the '$t' query. Anyway, $t is just a resource - not a string (file location). You need to call ... to get the path.
    Post Contains: Code
 
2
 

$sql doesn't exist. $s returns boolean (success/failure). What is the point of line 9?

If all the images are in the same folder, you can dispense with the '$t' query.
Anyway, $t is just a resource - not a string (file location).

You need to call mysql_fetch_assoc() to get the path.

 
0
 

First this is wrong:

if ($sql = $s){

here you are assignin a value, not comparing, to compare use == or for boolean ===, so rewrite it to:

if ($sql == $s){

Second, I don't see $sql declared anywhere, you will get an error here.
Third, you have to extract the filename from the query:

if(mysql_num_rows($t) > 0)
{
    # loop results
    while($row = mysql_fetch_object($t))
    {
        unlink('/path/'.$row->filename); # and extension if this is saved a part
    }
    header('Location: lee_remove.php');
}

bye!

 
0
 
pass your file name in a varialbe like
 $t = mysql_query("SELECT filename from image_gallery WHERE id IN ($p)");
$url=mysql_fetch_array($t);
$image=$url['filename'];  //what field you use in db type in the url index
then do
unlink($image);
this will delete the file from directory....
 
0
 

Hi thanks guys.

This is what I have now

if(isset($_POST['remove'])){
      $chk = (array) $_POST['remove'];
      $p = implode(',',array_keys($chk)); 
      $q = mysql_query("DELETE FROM image_gallery WHERE id IN ($p)");
      $s = mysql_query("DELETE FROM thumbs_gallery WHERE id IN ($p)");
      $t = mysql_query("SELECT * FROM image_gallery WHERE id IN ($p)");
      $url=mysql_fetch_array($t);
      $image=$url['filename'];  

      if ($s){

        unlink($image); 


      }
      else{
         echo 'There has been a problem. Go back and try again';
         echo "<br />";
         echo "<a href='lee_remove.php'>Back</a>";
      }
   }
   else{
         echo 'There are no images in the gallery';
         echo "<br />";
         echo "<a href='gallery_upload.php'>Add Images</a>";
      }

But I am getting this error
' Warning: unlink(): Invalid argument in C:\wamp\www\diamondback\lee_remove2.php on line 23' at unlink($image)

Any ideas?

 
0
 

Echo what is stored in $image. I'm pretty confident it's an empty string or null, because you first delete the id's in $p and then try to select them.

 
0
 

i am not test it but try this code it will work or not

if(isset($_POST['remove'])){
      $chk = (array) $_POST['remove'];
      $p = implode(',',array_keys($chk)); 

      $t = mysql_query("SELECT * FROM image_gallery WHERE id IN ($p)");
      $url=mysql_fetch_array($t);
      $image=$url['filename'];  
      if ($t){

        unlink($image); 
        $q = mysql_query("DELETE FROM image_gallery WHERE id IN ($p)");
      $s = mysql_query("DELETE FROM thumbs_gallery WHERE id IN ($p)");
      }
      else{
         echo 'There has been a problem. Go back and try again';
         echo "<br />";
         echo "<a href='lee_remove.php'>Back</a>";
      }
   }
   else{
         echo 'There are no images in the gallery';
         echo "<br />";
         echo "<a href='gallery_upload.php'>Add Images</a>";
      }
 
0
 

Thats what was happening, I was deleting the id first then tryin to unlink it after!

art18, your code worked a treat! Thank you.

Thanks everyone that reploied to this.

Glen...

Question Answered as of 2 Years Ago by AARTI SHRIVAS, pritaeas, diafol and 1 other
 
0
 

you are welcome glenrogers i m glad to help you.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article