CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Trying to edit an HTML file with my CGI

 



Cbush33
New User

Mar 8, 2013, 1:58 PM

Post #1 of 6 (611 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;


lightspd
Novice

Mar 8, 2013, 2:23 PM

Post #2 of 6 (609 views)
Re: [Cbush33] Trying to edit an HTML file with my CGI [In reply to] Can't Post

Well, first thing I notice is this.
$match == /Total Inventory Items: \d{0,10}/

change the == to =~

In perl == is numeric, =~ is for regex.


FishMonger
Veteran / Moderator

Mar 8, 2013, 4:01 PM

Post #3 of 6 (605 views)
Re: [Cbush33] Trying to edit an HTML file with my CGI [In reply to] Can't Post

Your script has a number of problems/mistakes, but the main one is the decision to manually edit the html file. DON'T DO THAT.

Use one of the template modules available on cpan such as HTML::Template. http://search.cpan.org/~wonko/HTML-Template-2.94/lib/HTML/Template.pm

Doing that will make it an extremely trivial matter to pass the updated $oldInventory value without having to worry about the parsing and updating of the file.


wickedxter
User

Mar 8, 2013, 4:06 PM

Post #4 of 6 (604 views)
Re: [Cbush33] Trying to edit an HTML file with my CGI [In reply to] Can't Post

rather then updateing the html file its self why not use a database rather it being a CSV (flat file) or SQL (Sqlite) CSV is the easist to todo. Then you can generate your gallery.html from whats in the database file.


Cbush33
New User

Mar 8, 2013, 11:08 PM

Post #5 of 6 (598 views)
Re: [wickedxter] Trying to edit an HTML file with my CGI [In reply to] Can't Post

Unfortunately, coding knowledge is extremely weak, and the SQL and template options, while I'm sure are more effective and efficient, look like greek to me :/ I've been able to fix all the random bugs in the code and get it to work -except- for the very last section of writing the html to the gallery.html file. The earlier opening and looking for the Inventory Item number works perfect, but apparently when it writes to the file at the end, something messes up and I end up with a 0 byte file.

Here's what I have right now:

my $file4 = "../gallery.html";
sub read_file {
my ($filename4) = shift;

open (FILE,">$filename4") or die "Can`t open $filename4 : $!";

while (<FILE>) {
$_ =~ s/<!-- End Inventory Item $oldInventoryItem -->/<!-- End Inventory Item $oldInventoryItem -->
<!-- Begin Inventory Item $newInventory -->
<div class="home_promo2">
<div class="home_promo_content2">
<table style="padding-left:20px">
<tr>
<td> <a href="images\/inventoryItem$newInventoryItem.jpg" rel="facebox"><img src="images\/inventoryItem$newInventoryItem.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;

}

close FILE;


The idea being that I open the file, and read it line by line, and change out any lines matching the old inventory end line with the new inventory item block. hence the While <FILE>, $_ =~ s/olditemend/olditemend+newblock/g;

I realize that there are much better ways to do what I'm trying to do, but this is literally the last step of the whole project, and once I figure it out I can sleep :/ I feel like if I do one of the other suggestions, It'd take me hours to understand why I'm changing things and to change them correctly.

Again, I appreciate all the help and advice


FishMonger
Veteran / Moderator

Mar 9, 2013, 6:32 AM

Post #6 of 6 (593 views)
Re: [Cbush33] Trying to edit an HTML file with my CGI [In reply to] Can't Post


Quote

Code
sub read_file { 
my ($filename4) = shift;

open (FILE,">$filename4") or die "Can`t open $filename4 : $!";


You're opening the file in write mode, which clobbers any data that it may have had.


Code
while (<FILE>) {

Here you're attempting to read from the file you just opened in write mode. That's not going to do anything useful.

You need to reverse those steps. Open the file in read mode and apply one or more regex's as you parse the data and place it into a data structure.

Once that parsing/updating is done, open the file in write mode and output the saved data from the data structure.

There are several variations of this type of approach, none of which I recommend.

Learning how to use a template will take less time than debugging the messy approach that you're currently attempting. All you need to do is make a few minor adjustments to your existing html file to turn it into a template and then in your script you'd make a few simple var assignments instead of the manual parsing that you're currently attempting.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives