Advice on deleting images from server using PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frankie

    #1

    Advice on deleting images from server using PHP

    Hello:

    I'm coding a website which allows the owner to make add/change/deletes to
    his product database, which includes product images stored separately on the
    server. I have coded the Delete page which deletes the product record from
    the database, however the product image remains in the associated folder on
    the server. I'd prefer these images not accumulate and would like to delete
    these images along with the database record.

    Can anyone refer me to a PHP script or suggest a way to do this?

    Frank H.
    Austin, TX


  • Krustov

    #2
    Re: Advice on deleting images from server using PHP

    <comp.lang.ph p>
    <Frankie>
    <Wed, 12 Jul 2006 18:36:01 GMT>
    <libtg.6765$ye3 .5139@newsread1 .news.pas.earth link.net>
    I'm coding a website which allows the owner to make add/change/deletes to
    his product database, which includes product images stored separately on the
    server. I have coded the Delete page which deletes the product record from
    the database, however the product image remains in the associated folder on
    the server. I'd prefer these images not accumulate and would like to delete
    these images along with the database record.
    >
    Can anyone refer me to a PHP script or suggest a way to do this?
    >
    $pass=0;
    $filename="imag es/whatever.jpg";
    if (!file_exists($ filename)) {$pass=1;}

    if ($pass==1) {unlink($filena me);}


    unlink() is a silly name for a delete command isnt it .


    --
    Encrypted email address

    Comment

    • Alvaro G. Vicario

      #3
      Re: Advice on deleting images from server using PHP

      *** Frankie escribió/wrote (Wed, 12 Jul 2006 18:36:01 GMT):
      I have coded the Delete page which deletes the product record from
      the database, however the product image remains in the associated folder on
      the server. I'd prefer these images not accumulate and would like to delete
      these images along with the database record.
      >
      Can anyone refer me to a PHP script or suggest a way to do this?


      Of course, you must read the filename from DB *before* deleting the record
      ;-)


      --
      -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      ++ Mi sitio sobre programación web: http://bits.demogracia.com
      +- Mi web de humor con rayos UVA: http://www.demogracia.com
      --

      Comment

      • Riemer Palstra

        #4
        Re: Advice on deleting images from server using PHP

        On 2006-07-12, Krustov <me@privacy.net wrote:
        unlink() is a silly name for a delete command isnt it .
        Why? That's exactly what it does - removing the link to a file (and
        probably decrementing the link count), isn't it? :)

        --
        Riemer Palstra Amsterdam, The Netherlands
        riemer@palstra. com http://www.palstra.com/

        Comment

        • Andrew Poelstra

          #5
          Re: Advice on deleting images from server using PHP

          On 2006-07-13, Riemer Palstra <riemer@palstra .comwrote:
          On 2006-07-12, Krustov <me@privacy.net wrote:
          >unlink() is a silly name for a delete command isnt it .
          >
          Why? That's exactly what it does - removing the link to a file (and
          probably decrementing the link count), isn't it? :)
          >
          It does a fair bit more than that, though. It also changes inode tables,
          changes directory listings, etc.

          --
          Andrew Poelstra <http://www.wpsoftware. net/projects/>
          To email me, use "apoelstra" at the above domain.
          "You people hate mathematics." -- James Harris

          Comment

          • Frankie

            #6
            Re: Advice on deleting images from server using PHP

            "Krustov" <me@privacy.net wrote in message
            news:MPG.1f1f53 0eb49767d989bd1 @news.newsreade r.com...
            >
            $pass=0;
            $filename="imag es/whatever.jpg";
            if (!file_exists($ filename)) {$pass=1;}
            >
            if ($pass==1) {unlink($filena me);}
            Thanks for your example! I like your code's simplicity. The documentation at
            php.net is difficult for me to follow.

            However, I need to use variables in this code.

            Specifically, I'm trying to do something like this:

            $pass=0;
            $filename="../{$_POST['hfCategory']}/photos/{$_POST['hfItemNumber']}.jpg";
            if (!file_exists($ filename)) {$pass=1;}

            if ($pass==1) {unlink($filena me);}

            ...but it's not working.

            Any additional advice appreciated.

            F.H.



            Comment

            • Krustov

              #7
              Re: Advice on deleting images from server using PHP

              <comp.lang.ph p>
              <Frankie>
              <Fri, 14 Jul 2006 02:53:51 GMT>
              <3HDtg.7339$ye3 .7309@newsread1 .news.pas.earth link.net>
              However, I need to use variables in this code.
              >
              Specifically, I'm trying to do something like this:
              >
              $pass=0;
              $filename="../{$_POST['hfCategory']}/photos/{$_POST['hfItemNumber']}.jpg";
              if (!file_exists($ filename)) {$pass=1;}
              >
              if ($pass==1) {unlink($filena me);}
              >
              ..but it's not working.
              >
              Any additional advice appreciated.
              >
              I'd start by breaking it down a little .

              $temp1="$_POST['hfCategory']";
              print "temp1 = $temp1 <br>";

              $temp2="$_POST['hfItemNumber']";
              print "temp2 = $temp2 <br>";

              $filename="$tem p1/photos/$temp2" . ".jpg";

              Making sure you have the path 100% correct is the first step .

              TMK "../" doesnt need to be used unless you have good reason to do so .


              --
              Encrypted email address

              Comment

              • Jerry Stuckle

                #8
                [OT] Your Sig

                Just a sidebar here.

                I normally don't pay attention to sigs. But I happened to glance at
                yours and saw your encrypted email address. I think that's a great
                idea. I'm going to have to see if there's something like that here in
                the states.

                Does it help stop spam?

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • Krustov

                  #9
                  Re: [OT] Your Sig

                  <comp.lang.ph p>
                  <Jerry Stuckle>
                  <Fri, 14 Jul 2006 12:30:22 -0400>
                  <Wa-dnWsYkfERWyrZnZ 2dnUVZ_qSdnZ2d@ comcast.com>
                  Just a sidebar here.
                  >
                  I normally don't pay attention to sigs. But I happened to glance at
                  yours and saw your encrypted email address. I think that's a great
                  idea. I'm going to have to see if there's something like that here in
                  the states.
                  >
                  Does it help stop spam?
                  >
                  You dont say what sig your talking about - but i'll take a wild guess
                  and assume its my own .

                  I think there one called 'cerbermail.com ' or summit that does the same
                  sort of thing if you dont mind google ads .

                  As for stopping spam - who knows .


                  --
                  Encrypted email address

                  Comment

                  • mark babli

                    #10
                    Re: Advice on deleting images from server using PHP

                    I just want to agree with Frankie on "going one step at a time" when
                    building any path on any file system. Moreover, simple echos to those
                    variable often save you lots of frustration. I was working on something
                    similar and had a heck of a time until I realized that the path isn't being
                    built right.


                    "Krustov" <me@privacy.net wrote in message
                    news:MPG.1f21bd b8fd6cf7f9989be a@news.newsread er.com...
                    <comp.lang.ph p>
                    <Frankie>
                    <Fri, 14 Jul 2006 02:53:51 GMT>
                    <3HDtg.7339$ye3 .7309@newsread1 .news.pas.earth link.net>
                    >
                    >However, I need to use variables in this code.
                    >>
                    >Specifically , I'm trying to do something like this:
                    >>
                    >$pass=0;
                    >$filename=". ./{$_POST['hfCategory']}/photos/{$_POST['hfItemNumber']}.jpg";
                    >if (!file_exists($ filename)) {$pass=1;}
                    >>
                    >if ($pass==1) {unlink($filena me);}
                    >>
                    >..but it's not working.
                    >>
                    >Any additional advice appreciated.
                    >>
                    >
                    I'd start by breaking it down a little .
                    >
                    $temp1="$_POST['hfCategory']";
                    print "temp1 = $temp1 <br>";
                    >
                    $temp2="$_POST['hfItemNumber']";
                    print "temp2 = $temp2 <br>";
                    >
                    $filename="$tem p1/photos/$temp2" . ".jpg";
                    >
                    Making sure you have the path 100% correct is the first step .
                    >
                    TMK "../" doesnt need to be used unless you have good reason to do so .
                    >
                    >
                    --
                    Encrypted email address
                    www.emailuser.co.uk/?name=KRUSTOV

                    Comment

                    • Geoff Berrow

                      #11
                      Re: [OT] Your Sig

                      Message-ID: <MPG.1f21dd75df 3e7827989bee@ne ws.newsreader.c omfrom
                      Krustov contained the following:
                      >Does it help stop spam?
                      >>
                      >
                      >You dont say what sig your talking about - but i'll take a wild guess
                      >and assume its my own .
                      Look at the headers.

                      Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
                      be able to live this down in troll circles.

                      --
                      Geoff Berrow (put thecat out to email)
                      It's only Usenet, no one dies.
                      My opinions, not the committee's, mine.
                      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: [OT] Your Sig

                        Krustov wrote:
                        <comp.lang.ph p>
                        <Jerry Stuckle>
                        <Fri, 14 Jul 2006 12:30:22 -0400>
                        <Wa-dnWsYkfERWyrZnZ 2dnUVZ_qSdnZ2d@ comcast.com>
                        >
                        >>Just a sidebar here.
                        >>
                        >>I normally don't pay attention to sigs. But I happened to glance at
                        >>yours and saw your encrypted email address. I think that's a great
                        >>idea. I'm going to have to see if there's something like that here in
                        >>the states.
                        >>
                        >>Does it help stop spam?
                        >>
                        >
                        >
                        You dont say what sig your talking about - but i'll take a wild guess
                        and assume its my own .
                        >
                        I think there one called 'cerbermail.com ' or summit that does the same
                        sort of thing if you dont mind google ads .
                        >
                        As for stopping spam - who knows .
                        >
                        >
                        Yep, I figured since I was replying to you it would be obvious. Guess
                        not :-).

                        Thanks of the tip on cerbermail.com - but think I'll skip the google
                        ads. Maybe put it on one of my own sites.

                        --
                        =============== ===
                        Remove the "x" from my email address
                        Jerry Stuckle
                        JDS Computer Training Corp.
                        jstucklex@attgl obal.net
                        =============== ===

                        Comment

                        • Krustov

                          #13
                          Re: [OT] Your Sig

                          <comp.lang.ph p>
                          <Geoff Berrow>
                          <Sat, 15 Jul 2006 01:43:14 +0100>
                          <3degb2leafguap bd2hp9oesbabaih fkh9b@4ax.com>
                          Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
                          be able to live this down in troll circles.
                          >
                          Doubt his own version of it will be as good as mine :-)


                          --
                          Encrypted email address

                          Comment

                          • Frankie

                            #14
                            Re: Advice on deleting images from server using PHP

                            "Krustov" <me@privacy.net wrote in message
                            news:MPG.1f21bd b8fd6cf7f9989be a@news.newsread er.com...
                            >
                            I'd start by breaking it down a little .
                            >
                            $temp1="$_POST['hfCategory']";
                            print "temp1 = $temp1 <br>";
                            >
                            $temp2="$_POST['hfItemNumber']";
                            print "temp2 = $temp2 <br>";
                            >
                            $filename="$tem p1/photos/$temp2" . ".jpg";
                            >
                            Making sure you have the path 100% correct is the first step .
                            >
                            TMK "../" doesnt need to be used unless you have good reason to do so .
                            Thanks again for your help.

                            I have found the error. Your original code's conditional statement read:

                            if (!file_exists($ filename)) {$pass=1;}

                            I missed the "!", so it would only delete the file if it did NOT exist. But
                            it does.

                            I have since corrected and simplified the code to this:

                            $pass=0;
                            $photo="../$hfCategory/photos/$hfItemNumber.j pg";
                            if (file_exists($p hoto)) {$pass=1;}

                            if ($pass==1) {unlink($photo) ;}

                            ...and it works beautifully.

                            BTW: in my case, the "../" in the file path IS required in order to locate
                            the file. The actual sample file path is thus:
                            "../lifestyle/photos/6000.jpg"

                            F.H.


                            Comment

                            • Sheldon Glickler

                              #15
                              Re: [OT] Your Sig


                              "Krustov" <me@privacy.net wrote in message
                              news:MPG.1f22c2 e1228d079c989bf 3@news.newsread er.com...
                              <comp.lang.ph p>
                              <Geoff Berrow>
                              <Sat, 15 Jul 2006 01:43:14 +0100>
                              <3degb2leafguap bd2hp9oesbabaih fkh9b@4ax.com>
                              >
                              >Bloody hell krusty, Jerry Stuckle says you have a good idea. You'll not
                              >be able to live this down in troll circles.
                              >>
                              >
                              Doubt his own version of it will be as good as mine :-)
                              >
                              If you REALLY want to eliminate ALL spam with ZERO false positives go look
                              at www.validmail.net. Except during a server upgrade and mismatch, I
                              haven't received spam from there in over a year and a half.

                              Shellyh


                              Comment

                              Working...