473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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:

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
13 1731
numberwhun
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
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
3,509 Expert Mod 2GB
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
How would I do that?
Aug 21 '07 #7
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
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
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
3,509 Expert Mod 2GB
DOH!!! Jeff (like Dobby) must punish himself for that blind miss.
Aug 22 '07 #11
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
3,509 Expert Mod 2GB
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
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

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

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
1
by: Matt Hamilton | last post by:
I have a simple image gallery where I want to allow users to delete files. The problem I have is that after an image is displayed in the browser, I am not able to delete the file because "The...
5
by: Velvet | last post by:
Can someone tell me to what process I need to attach to be able to step through my classic ASP code in VS.net 2003. I'm working on an XP box with IIS installed. I also have VS.net 2005 (The...
0
by: Ed | last post by:
All of a sudden my previously working code started throwing this error. from the SqlDatasource. I am using C# and Asp.net 2.0. Getting the following error: You have specified that your delete...
0
by: Arpan | last post by:
Suppose I have the following DataList code: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) ................... ................... ......................
6
by: Rob R. Ainscough | last post by:
I can't seem to locate the appropriate area in VS 2005 where I can accomplish this -- is this a C# only option? Thanks, Rob.
4
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
6
by: DavidPr | last post by:
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...
0
by: Peted | last post by:
I suspect there is a better forum for this question, but i couldnt find it. If someone could point me in the right direction or help me with this question it would be muchly appreciated I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.