1,105,286 Community Members

Unlink() help

Member Avatar
GlenRogers
Posting Whiz
329 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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...............

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 90 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
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!

Member Avatar
AARTI SHRIVAS
Posting Pro in Training
410 posts since Dec 2012
Reputation Points: 2 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 3 [?]
 
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....
Member Avatar
GlenRogers
Posting Whiz
329 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
pritaeas
mod_pritaeas
11,287 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,832 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
AARTI SHRIVAS
Posting Pro in Training
410 posts since Dec 2012
Reputation Points: 2 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 3 [?]
 
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>";
      }
Member Avatar
GlenRogers
Posting Whiz
329 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 1 Year Ago by AARTI SHRIVAS, cereal, pritaeas and 1 other
Member Avatar
AARTI SHRIVAS
Posting Pro in Training
410 posts since Dec 2012
Reputation Points: 2 [?]
Q&As Helped to Solve: 38 [?]
Skill Endorsements: 3 [?]
 
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