Delete image from server using php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    #1

    Delete image from server using php?

    I made a little picture gallery for my website. The images and thumbnails are stored on the server and the information in a database. I know how to delete the information from the databasem, but how would I include instructions in the php script to also remove the thumb and image from the server?

    Code:
    <?php
    if($_GET["cmd"]=="delete")
    {
        $sql = "DELETE FROM $table WHERE id=$id";
    
    // code to delete images from server
    
        $result = mysql_query($sql);
    
    echo "<br><br><p align='center'><span style='color:red'><b>The Image and Information has been Deleted!<br><br>";
    }
    ?>
  • digitalpbk
    New Member
    • Jun 2007
    • 4

    #2
    Before deleting the RECORD. Delete the files
    try the unlink()
    delete() functions ...
    i hope you have the file locations stored on the database

    fetch it delete the files
    delete the record.

    Comment

    • DavidPr
      New Member
      • Mar 2007
      • 155

      #3
      No, I don't have the location of the image files stored in the database? Is this a good idea? If so, how would I show or store the path to images? I'd have a field named img_url and have the location like this: (/home/pictures/image_uploads/)

      To unlink a file I would use this: [PHP]unlink("$img_ur l/tree.jpg");[/PHP]

      How would I tie this in with a delete script that removes the information regarding this image from the database?

      Comment

      • bonski
        New Member
        • Jun 2007
        • 53

        #4
        Originally posted by DavidPr
        No, I don't have the location of the image files stored in the database? Is this a good idea? If so, how would I show or store the path to images? I'd have a field named img_url and have the location like this: (/home/pictures/image_uploads/)

        To unlink a file I would use this: [PHP]unlink("$img_ur l/tree.jpg");[/PHP]

        How would I tie this in with a delete script that removes the information regarding this image from the database?

        ei david,

        it doesn't matter if you dont have you directory paths.. as long as you know where your images are located... and most importanty the image filename should be save in the database..

        ok... how you would tie it together with delete script.. heres how..

        Code:
        $img_dir = 'image_directory_name/';
        $img_thmb = 'thumbnail_directory_name/';// if you had thumbnails
        
        $image_name = $row['image_name'];//assume that this is the image_name field from your database
        
        //unlink function return bool so you can use it as conditon
        if(unlink($img_dir.$image_name) && unlink($img_thmb.$image_name)){
            //assume that variable $image_id is queried from the database where your image record your about to delete is...
            $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
            $qry = mysql_query($sql);
        }else{
           echo 'ERROR: unable to delete image file!';
        }
        ok? have fun... ^___^

        bonski

        Comment

        • DavidPr
          New Member
          • Mar 2007
          • 155

          #5
          Thank you for the example.

          Code:
          $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
          May I ask why you placed the image_id like this'".$image_id ."'instead of like this '$image_name'?

          Comment

          • bonski
            New Member
            • Jun 2007
            • 53

            #6
            Originally posted by DavidPr
            Thank you for the example.

            Code:
            $sql = "DELETE FROM table WHERE image_id = '".$image_id."'";
            May I ask why you placed the image_id like this'".$image_id ."'instead of like this '$image_name'?
            oh... well i'm just used to do it like that... its a concatenation.. . notice the dot(.)

            well, i guess... they are just the same.. im just doing it like that so that its easy for me to trace... ^___^

            ok... your welcome...!

            Comment

            • jayrama
              New Member
              • Jan 2010
              • 1

              #7
              Dont know what I'm missing...need help!

              Here's the code that I'm trying to use from this post..

              Code:
              <?php include('dbconnect.php');
              
              $img_dir = '../images/ads/';
              $img_thmb = '../images/ads/sml_';
              
              $ad_photo = $row['ad_photo'];
              
              //unlink function return bool so you can use it as conditon
              if(@unlink($img_dir.$ad_photo) && @unlink($img_thmb.$ad_photo)){
              $sql = "DELETE FROM pbsl_ads WHERE ad_id = '".$ad_id."'";
              $qry = mysql_query($sql);
              }else{
              echo 'ERROR: unable to delete image file!, <a href=index.php> back</a>';
              }
              
              ?>
              The image wont delete from the server. I'm able to delete info from database.

              This is what I have in my db table:

              Code:
              `ad_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
              `ad_date` VARCHAR(60) NOT NULL,
              `ad_order` INT(3) NOT NULL,
              `ad_name` TEXT NOT NULL,                     // this is the name of the person
              `ad_info` TEXT NOT NULL,
              `ad_email` TEXT NOT NULL,
              `ad_photo` VARCHAR(50) NOT NULL,       // this is the photo
              `ad_cardphoto` VARCHAR(50) NOT NULL  // this is the business card photo
              Thanks for any help
              Last edited by Dormilich; Jan 1 '10, 11:19 AM. Reason: Please use [code] tags when posting code

              Comment

              Working...