Debugging CGI App - Delete image from server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deppeler
    New Member
    • Jul 2007
    • 40

    Debugging CGI App - Delete image from server

    I have a script that allows the user to upload images to the server & writes the file name to a flat file DB.
    I have another script that allows the user to delete the image entry from the DB.
    Is there a way to also delete the image from the server folder as well?

    Here is the script I am using to delete the entry in the DB:

    [CODE=perl]
    if ($input{'action '} eq 'delete'){
    open (dbk,">$dbk");
    @DB = <dbk>;
    foreach $rec (@ODB){
    chomp($rec);
    ($nktype,$nkcod e,) = split(/\,/,$rec);
    if ($nktype eq $input{'nktype' } && $nkcode eq $input{'nkcode' }) {
    print dbk "";
    } else {
    print dbk "$nktype,$nkcod e,\n";
    }
    }
    close (dbk);
    }[/CODE]

    I know that possibly it is the 'unlink' function would do it, but how would I incorporate it into this code?


    I tried this:
    [CODE=perl]
    $dbk='/home/mysite/db/dbp.txt';
    $loc='/home/mysite/www/toypics/';

    if ($input{'action '} eq 'delete') {
    open (dbk,">$dbk");
    @DB = <dbk>;
    $file = '$loc$nktype';
    foreach $rec (@ODB) {
    chomp($rec);
    ($nktype,$nkcod e,)=split(/\,/,$rec);
    if ($nktype eq $input{'nktype' } && $nkcode eq $input{'nkcode' }) {
    unlink($file);
    print dbk "";
    } else {
    print dbk "$nktype,$nkcod e,\n";
    }
    }
    close (dbk);
    }[/CODE]

    thanks
    Paul
    Last edited by miller; Aug 22 '07, 03:47 PM. Reason: Reformatted Code
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    One thing that I see is that you are defining $file before you actually set $nktype to be "anything".

    What error are you seeing, if any?

    Regards,

    Jeff

    Comment

    • deppeler
      New Member
      • Jul 2007
      • 40

      #3
      I am not seeing any error and I have the first script (where the users choose the image) auto reload on delete, the image name is being deleted from the DB but still not on the server.

      Paul

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        As a matter of Best Practice's, make sure that your file handles are all UPPER case, so they can be quickly distinguished from the rest of your code (i.e.: s/dbk/DBK/).

        As I mentioned in my last post, it looks like you are trying to use the variable $nktype in the definition of $file, before $nktype is even set. That may be your issue. Set $file after you set $nktype, otherwise, it will have a null value if its used prior to its own assignment.

        Regards,

        Jeff

        Comment

        • deppeler
          New Member
          • Jul 2007
          • 40

          #5
          Ok I tried this but it didn't work:

          [CODE=perl]
          $DBK = '/home/childcar/db/dbp.txt';
          $loc = '/home/childcar/www/toypics/';

          if ($input{'action '} eq 'delete') {
          open (DBK,">$DBK");
          @DB = <DBK>;
          foreach $rec (@ODB){
          chomp($rec);
          ($nktype, $nkcode,) = split(/\,/,$rec);
          $file='$loc$nkt ype';
          if ($nktype eq $input{'nktype' } && $nkcode eq $input{'nkcode' }) {
          unlink($file);
          print DBK "";
          }else{
          print DBK "$nktype,$nkcod e,\n";
          }
          }
          close (DBK);
          }
          [/CODE]

          thanks
          Last edited by miller; Aug 22 '07, 04:19 PM. Reason: Code Tag and ReFormatting

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Well, the only thing you can do is start setting up for troubleshooting . I would print out the variables that make up $file as well as $file to ensure that everything is getting set correctly.

            Also, for your reference only, here is a link to the Perl Best Practices Reference Card. In lieu of buying the book (which is a good read for those interested), this can suffice as a guide during coding.

            Regards,

            Jeff

            Comment

            • deppeler
              New Member
              • Jul 2007
              • 40

              #7
              How would I do that?

              Comment

              • deppeler
                New Member
                • Jul 2007
                • 40

                #8
                If I use this:

                [CODE=perl]
                $DBK = '/home/childcar/db/dbp.txt';
                $databaseview = 'http://www.mysite.org/cgi-bin/admintoy/photo.pl';

                if ($input{'action '} eq 'delete'){
                open (DBK,">$DBK");
                @DB = <DBK>;
                foreach $rec (@ODB){
                chomp($rec);
                ($nktype,$nkcod e,) = split(/\,/,$rec);
                if ($nktype eq $input{'nktype' } && $nkcode eq $input{'nkcode' }) {
                print DBK "";
                } else {
                print DBK "$nktype,$nkcod e,\n";
                }
                }
                close (DBK);
                }
                $file = '/home/childcar/www/toypics/art.gif';
                unlink($file);
                print "Location: $databaseview\n \n";
                [/CODE]

                If I sub 'art.gif' in $file with $nktype it doesn't work

                If I replace $databaseview with $nktype it prints art.gif
                If I replace $file 'art.gif' with $nktype and replace $databaseview with $file it prints $nktype

                Any ideas...it's driving me nuts

                thanks
                Last edited by miller; Aug 22 '07, 04:22 PM. Reason: Code Tag and ReFormatting

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  Quick question. Is this ALL of your code? Reason I ask is, I do not see that you have "use strict;" and "use warnings;". Those will typically force you to correct any and all syntactical and other errors before your code will work. If they aren't on, please turn them on and run the script. Then, correct any messages they print and we can go on from there.

                  (Sorry, all part of troubleshooting . There aren't many people here, myself included, who want to troubleshoot a script that hasn't been passed through those two pragmas. )

                  Regards,

                  Jeff

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    look here:


                    $file='$loc$nkt ype';

                    $file will literally equal $loc$nktype because of the single-quotes, use double-quotes to construct strings from scalars or use concatenation:

                    $file="$loc$nkt ype";
                    $file=$loc . $nktype";


                    Jeff - 1 demerit for not spotting the single-quotes ;)

                    Comment

                    • numberwhun
                      Recognized Expert Moderator Specialist
                      • May 2007
                      • 3467

                      #11
                      DOH!!! Jeff (like Dobby) must punish himself for that blind miss.

                      Comment

                      • deppeler
                        New Member
                        • Jul 2007
                        • 40

                        #12
                        Ok I have it working, thanks for the primer...BUT I have one problem

                        When it deletes the file from the server it deletes the LAST file uploaded and not the correct file, any ideas?

                        I used this code:
                        Code:
                        my $file = "/home/childcar/www/toypics/$nktype";
                        unlink($file);
                        thanks

                        Comment

                        • numberwhun
                          Recognized Expert Moderator Specialist
                          • May 2007
                          • 3467

                          #13
                          I would do some tests (with the unlink commented out) and make sure that the $file variable is getting set to the correct file name.

                          Regards,

                          Jeff

                          Comment

                          • deppeler
                            New Member
                            • Jul 2007
                            • 40

                            #14
                            I worked it out.

                            I just had to move:

                            Code:
                            my $file = '/home/childcar/www/toypics/$nktype;
                            unlink($file);
                            to before this:

                            Code:
                            print DBK "";
                            }else{
                            print DBK "$nktype,$nkcode,\n";
                            }
                            thanks!

                            Comment

                            Working...