By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,467 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,467 IT Pros & Developers. It's quick & easy.

Debugging CGI App - Delete image from server

P: 40
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:

Expand|Select|Wrap|Line Numbers
  1. if ($input{'action'} eq 'delete'){
  2.     open (dbk,">$dbk");
  3.     @DB = <dbk>;
  4.     foreach $rec (@ODB){
  5.         chomp($rec);
  6.         ($nktype,$nkcode,) = split(/\,/,$rec);
  7.         if ($nktype eq $input{'nktype'} && $nkcode eq $input{'nkcode'}) {
  8.             print dbk "";
  9.         } else {
  10.             print dbk "$nktype,$nkcode,\n";
  11.         }
  12.     }
  13.     close (dbk);
  14. }
I know that possibly it is the 'unlink' function would do it, but how would I incorporate it into this code?


I tried this:
Expand|Select|Wrap|Line Numbers
  1. $dbk='/home/mysite/db/dbp.txt';
  2. $loc='/home/mysite/www/toypics/';
  3.  
  4. if ($input{'action'} eq 'delete') {
  5.     open (dbk,">$dbk");
  6.     @DB = <dbk>;
  7.     $file = '$loc$nktype';
  8.     foreach $rec (@ODB) {
  9.         chomp($rec);
  10.         ($nktype,$nkcode,)=split(/\,/,$rec);
  11.         if ($nktype eq $input{'nktype'} && $nkcode eq $input{'nkcode'}) {
  12.             unlink($file);
  13.             print dbk "";
  14.         } else {
  15.             print dbk "$nktype,$nkcode,\n";
  16.         }
  17.     }
  18.     close (dbk);
  19. }
thanks
Paul
Aug 21 '07 #1
Share this Question
Share on Google+
13 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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
Aug 21 '07 #2

P: 40
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
Aug 21 '07 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Aug 21 '07 #4

P: 40
Ok I tried this but it didn't work:

Expand|Select|Wrap|Line Numbers
  1. $DBK = '/home/childcar/db/dbp.txt';
  2. $loc = '/home/childcar/www/toypics/';
  3.  
  4. if ($input{'action'} eq 'delete') {
  5.     open (DBK,">$DBK");
  6.     @DB = <DBK>;
  7.     foreach $rec (@ODB){
  8.         chomp($rec);
  9.         ($nktype, $nkcode,) = split(/\,/,$rec);
  10.         $file='$loc$nktype';
  11.         if ($nktype eq $input{'nktype'} && $nkcode eq $input{'nkcode'}) {
  12.             unlink($file);
  13.             print DBK "";
  14.         }else{
  15.             print DBK "$nktype,$nkcode,\n";
  16.         }
  17.     }
  18.     close (DBK);
  19. }
  20.  
thanks
Aug 21 '07 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Aug 21 '07 #6

P: 40
How would I do that?
Aug 21 '07 #7

P: 40
If I use this:

Expand|Select|Wrap|Line Numbers
  1. $DBK = '/home/childcar/db/dbp.txt';
  2. $databaseview = 'http://www.mysite.org/cgi-bin/admintoy/photo.pl';
  3.  
  4. if ($input{'action'} eq 'delete'){
  5.     open (DBK,">$DBK");
  6.     @DB = <DBK>;
  7.     foreach $rec (@ODB){
  8.         chomp($rec);
  9.         ($nktype,$nkcode,) = split(/\,/,$rec);
  10.         if ($nktype eq $input{'nktype'} && $nkcode eq $input{'nkcode'}) {
  11.             print DBK "";
  12.         } else {
  13.             print DBK "$nktype,$nkcode,\n";
  14.         }
  15.     }
  16.     close (DBK);
  17. }
  18. $file = '/home/childcar/www/toypics/art.gif';
  19. unlink($file);
  20. print "Location: $databaseview\n\n";
  21.  
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
Aug 21 '07 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Aug 21 '07 #9

KevinADC
Expert 2.5K+
P: 4,059
look here:


$file='$loc$nktype';

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

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


Jeff - 1 demerit for not spotting the single-quotes ;)
Aug 22 '07 #10

numberwhun
Expert Mod 2.5K+
P: 3,503
DOH!!! Jeff (like Dobby) must punish himself for that blind miss.
Aug 22 '07 #11

P: 40
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:
Expand|Select|Wrap|Line Numbers
  1. my $file = "/home/childcar/www/toypics/$nktype";
  2. unlink($file);
  3.  
thanks
Aug 22 '07 #12

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Aug 22 '07 #13

P: 40
I worked it out.

I just had to move:

Expand|Select|Wrap|Line Numbers
  1. my $file = '/home/childcar/www/toypics/$nktype;
  2. unlink($file);
  3.  
to before this:

Expand|Select|Wrap|Line Numbers
  1. print DBK "";
  2. }else{
  3. print DBK "$nktype,$nkcode,\n";
  4. }
thanks!
Aug 22 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.