
Cbush33
New User
Mar 8, 2013, 1:58 PM
Post #1 of 6
(238 views)
|
|
Trying to edit an HTML file with my CGI
|
Can't Post
|
|
Hi, the CGI is designed to take data from a post request (which works fine), and then use that data to upload an image to a separate directory, and edit an html which will display that image and information (this is basically an inventory update script). The file upload seems to work perfectly, and the HTML part of this CGI works great and shows the uploaded image and all the data just fine, but the gallery.html file remains the same, as if the CGI file is either not trying to edit the file, or cannot. The script is also designed to get the Total Inventory count from a comment in the gallery.html file, but I dont know if that is working because the rest isn't working. This may be more of a permissions problem than a Perl problem, but I've even tried chmodding the gallery.html to 777 temporarily to see if that resolved the issue (which it didn't). Any advice on what I may be doing wrong and why the script isn't writing to the html file would be greatly appreciated. Thanks! #!/usr/bin/perl -wT use CGI; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; my $cgi = CGI->new; print $cgi->header('text/html'); $CGI::POST_MAX = 1024 * 10000; my $safe_filename_characters = "a-zA-Z0-9_.-"; my $upload_dir = "../images"; my $newInventory = 0; my $oldInventory = 0; my $query = new CGI; my $newItemPhoto = $query->param("newItemPhoto"); my $newItemName = $query->param("newItemName"); my $newItemDescription = $query->param("newItemDescription"); my $newItemPrice = $query->param("newItemPrice"); if ( !$newItemPhoto ) { print $query->header ( ); print "There was a problem uploading your photo (try a smaller file)."; exit; } my ( $name, $path, $extension ) = fileparse ( $newItemPhoto, '\..*' ); $newItemPhoto = "InventoryItem" & $newInventory . $extension; $newItemPhoto =~ tr/ /_/; $newItemPhoto =~ s/[^$safe_filename_characters]//g; if ( $newItemPhoto =~ /^([$safe_filename_characters]+)$/ ) { $newItemPhoto = $1; } else { die "Filename contains invalid characters"; } my $file2 = "../gallery.html"; sub read_file { my ($filename2) = shift; my @lines; open (FILE3, "$filename2") or die "Can`t open $filename2 : $!"; while (<FILE3>) { push @lines, $_; } close FILE3; return @lines; } my @file2 = &read_file($file2); foreach my $match (@file2) { if ($match == /Total Inventory Items: \d{0,10}/) { $match =~ /(\d+)/; $newInventory = $match + 1; } } my $upload_filehandle = $query->upload("newItemPhoto"); print " " & $newItemPhoto & " "; open ( UPLOADFILE, ">$upload_dir/InventoryItem$newInventory" ) or die "$!"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE; print $query->header ( ); print "Content-type: text/html\n\n"; print <<END_HTML; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Successful Inventroy Update</title> <style type="text/css"> img {border: none;} </style> </head> <body> <p>The Inventory Item $newItemName has been succesfully added.</p> <p>Item Description: $newItemDescription</p> <p>Item Price: $newItemPrice</p> <p><img src="/images/$newItemPhoto" alt="Photo" /></p> </body> </html> END_HTML ; $oldInventory = $newInventory - 1; open (IN, "../gallery.html"); @file = <IN>; seek IN,0,0; foreach $file (@file){ $file =~ s/<!-- End Inventory Item $oldInventory -->/<!-- End Inventory Item $oldInventory --> <!-- Begin Inventory Item $newInventory --> <div class="home_promo2"> <div class="home_promo_content2"> <table style="padding-left:20px"> <tr> <td> <a href="images\/inventoryItem$newInventory.jpg" rel="facebox"><img src="images\/inventoryItem$newInventory.jpg" width="200"><\/a><\/td> <td style="padding-left:20px"> <h2>$newItemName<\/h2> <p>$newItemDescription<\/p> <p>$newItemPrice<\/p> <\/td><\/tr> <\/table><br> <\/div> <\/div> <!-- End Inventory Item $newInventory -->/g; print IN $file; } close IN;
|