We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Unlink() help

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...............

5
Contributors
8
Replies
3 Days
Discussion Span
4 Months Ago
Last Updated
9
Views
Question
Answered
GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

$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.

diafol
Keep Smiling
Moderator
10,653 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,510
Skill Endorsements: 57

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!

cereal
Veteran Poster
1,145 posts since Aug 2007
Reputation Points: 344
Solved Threads: 222
Skill Endorsements: 22
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....
arti18
Posting Whiz in Training
207 posts since Dec 2012
Reputation Points: 2
Solved Threads: 25
Skill Endorsements: 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?

GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

pritaeas
Posting Prodigy
Moderator
9,287 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,458
Skill Endorsements: 86

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>";
      }
arti18
Posting Whiz in Training
207 posts since Dec 2012
Reputation Points: 2
Solved Threads: 25
Skill Endorsements: 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...

GlenRogers
Posting Whiz in Training
256 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Months Ago by arti18, diafol, cereal and 1 other

you are welcome glenrogers i m glad to help you.

arti18
Posting Whiz in Training
207 posts since Dec 2012
Reputation Points: 2
Solved Threads: 25
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1070 seconds using 2.95MB