Current location: Hot Scripts Forums » Programming Languages » Perl » HTML code file update on the webserver using CGI script


HTML code file update on the webserver using CGI script

Reply
  #1 (permalink)  
Old 05-28-06, 10:53 PM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
HTML code file update on the webserver using CGI script

Hi!

I have created a CGI script, which open files from the web-server using query string, file like .shtml, .html etc. I have made it for instant update, of html or shtml to save the time of download/upload file each time of updation, of small code or put static data on regular basis. After made all changes it will save the code with it original file name.

But the problem is while saving the file, it’s putting extra blank lines after each line of the code and due to that file size get increased day after day, when we update a .shtml or .html file each time. We can find this problem when we open those file on local editor find many blank after each line of code.

Can any one have any idea or suggestion, on how can I solve this problem.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 05-29-06, 05:19 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
your script is adding extra newlines, thats the problem. Can't help further without seeing your perl code.
Reply With Quote
  #3 (permalink)  
Old 05-29-06, 11:01 PM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Here is a part of the code:

Code:
#!/usr/bin/perl -w

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
$query = new CGI;

$file3 = "/path to my web directory/$ENV{'QUERY_STRING'}";

$act = $query->param('action');

unless ($act) {

if (-e $file3) {

open (TEMP,"$file3") or die "Can't Open $file3: $!\n";
@lines = <TEMP>;
chop(@line);
close(TEMP);
}

else {
@lines = "file does not exists";
}

&print_form;

}

if ($act eq "Save Changes") 
{
&get_results;
&display_HTML;
&file_copy;
}

sub get_results {

$subject = $query->param('subject');
chop($subject);


open(DATAF,"<$filename4") || die "Can't Open $filename4: $!\n";
$serial_no = <DATAF>;
close(DATAF);
if ($serial_no ne "")
{
  $serial_no = "";
}

#### write the new entry
#-----------------------
open(DATAF,">$filename4") || die "Can't Open $filename4: $!\n";
print DATAF ($serial_no);
close(DATAF);


#### write the new entry
#-----------------------
open(DATAF,">$filename4") or die "Can't Open $filename4: $!\n";
print DATAF ($subject);
close(DATAF);
}


sub print_form {
print "Content-type: text/html\n\n";
print <<"HTML";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><title>File Edit-Form</title>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<style>
A.b {text-decoration:none}
A.b:hover, A.b:active{text-color:#00FF00; text-decoration:none}
</style>
<style type="text/css">
a
{text-decoration: none;}
.title
{position: absolute;
width: 150px;
height: 10px;
left: 4px;
z-index: 10;
font-family: verdana, helvetica, sans-serif;
font-weight: bold;
font-size: 11.5px;}

.submenu
{position: absolute;
left: 4px;
width: 145px;
border: 1px solid green;
background-color: #007942;
layer-background-color: #007942;
font-family: verdana, helvetica, sans-serif;
font-size: 10px;
color: white;
text-decoration:none;
visibility: hidden;}

.submenu a{
text-decoration:none;
color: white;
}

.title a{
text-decoration:none;
color: white;
}
.title a:hover{
color:#ffffaa
}

.submenu a:hover{
color:#00ff00
}
</style>
</head>
<body link="#0000FF" alink="#0000ff" vlink="#0000ff" bgcolor="#FFFFFF" background="/images/b.gif"  marginwidth="1" marginheight="1" style="margin: 1">

 @header
@menu

<p align="right"><font color="#008000" size="5" style="font-family: verdana; font-weight: bold"><em>File Edit-Form<br>
    <img src="/images/green.gif" width="100%" height="2" alt=""></em></font></p>

<FORM name=form method=post align="center">
<table bgcolor="fff5f5" border="0" cellpadding="5" cellspacing="0" bgcolor="#ffffff" width="95%">
  <TBODY>
  <TR>
  <tr>
     <td width="200">SHTML CODE FILE: <br>

<TEXTAREA name=\"subject\" rows=22 wrap=virtual cols=100>@lines</TEXTAREA></TD></TR>

<TR>
 <td width="800" align="center" valign="top">
<p align="left">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT type="submit" value="Create New File" name=action>&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT type="submit" value="Save Changes" name=action></p>
<CENTER>
<p>&nbsp;</p>
</CENTER>
</TD></TR>
</TBODY></TABLE>
</FORM>
<BR><BR>

@footer;
</BODY></HTML>
HTML
exit;
}

sub file_copy {
use File::Copy;
copy("/pah to my web directory/fxshtmlcode.dat", "/pah to my web directory/$ENV{'QUERY_STRING'}") or die "copy failed: $!";

}

sub display_HTML
{
XXXXXXX
}
hope this help you guys to find out.

Thanks.
Reply With Quote
  #4 (permalink)  
Old 05-30-06, 01:30 AM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
you got some funny stuff in the code, like this:

Code:
@lines = <TEMP>;
chop(@line);
note you have @lines but you chop @line (no 's' on the end)

you probably should be using chomp anyway, not chop:

Code:
@lines = <TEMP>;
chomp(@lines);
That might solve your problem if you use chomp and chomp the correct array: @lines. You should be using "use strict;" with your perl scripts and it would have caught the @lines/@line problem.

this makes no sense to me:

Code:
#### write the new entry
#-----------------------
open(DATAF,">$filename4") || die "Can't Open $filename4: $!\n";
print DATAF ($serial_no);
close(DATAF);


#### write the new entry
#-----------------------
open(DATAF,">$filename4") or die "Can't Open $filename4: $!\n";
print DATAF ($subject);
close(DATAF);
}
first you overwrite $filename4 with $serial_no then you immediately overwrite it again with $subject.
Reply With Quote
  #5 (permalink)  
Old 05-31-06, 12:03 AM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Why not change your code so that it creates a back-up file (for emergency purposes...)

--------------------------------------------------------------
open(DATAF,"<$filename4") || die "Can't Open $filename4: $!\n";
$serial_no = <DATAF>;
close(DATAF);
if ($serial_no ne "")
{
#Here we create a back up in case we make an unwanted change.
#We should create a few more lines of code to clean out this directory
#as we don't need these old files hanging around.
open(DATAFB,">>$filename4.BAK") || die "Can't Open $filename4.BAK: $!\n";
print DATAFB ($serial_no);
close(DATAFB);
}


#### write the new entry
#-----------------------
open(DATAF,">$filename4") or die "Can't Open $filename4: $!\n";
print DATAF ($subject);
close(DATAF);
}

Last edited by curbview.com; 05-31-06 at 12:05 AM. Reason: To delete un-needed lines of code
Reply With Quote
  #6 (permalink)  
Old 05-31-06, 02:57 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks both of guys for your help and concern!

i am using the same .dat file for storing the data for every files which i want to edit.

--------------------

open(DATAF,"<$filename4") || die "Can't Open $filename4: $!\n";
$serial_no = <DATAF>;
close(DATAF);
if ($serial_no ne "")
{
$serial_no = "";
}

open(DATAF,">$filename4") || die "Can't Open $filename4: $!\n";
print DATAF ($serial_no);
close(DATAF);


#### write the new entry
#-----------------------
open(DATAF,">$filename4") or die "Can't Open $filename4: $!\n";
print DATAF ($subject);
close(DATAF);
}
-----------------------------------

so that first i clean up the old code and them right down the current changes into the file. i don't take a back of the older file. but now i will do.

but after Millennium's question its strick me, and i have test with following and find this is working perfectly without cleaning the older data.

------------------

#### write the new entry
#-----------------------
open(DATAF,">$filename4") or die "Can't Open $filename4: $!\n";
print DATAF ($subject);
close(DATAF);
}
-------------------

its solve the purpose.

and regarding

@lines = <TEMP>;
chomp(@lines);

i am still observing the things is working perfectly or not. its take some days to find out.

should use this also, because i am writing this variable into the .dat file and save it.

chomp($subject);

should i use this also??? please tell me.

one more thing i have found that, it store the whole code into center of the file, i realize that when i open the file on editor. find each and everyline is on the center alinged, which is generally on left side on the editor, when we start code or edit code.

can any one of you tell me why is this happening?

Thanks guys.

Last edited by sujata_ghosh; 05-31-06 at 03:01 AM.
Reply With Quote
  #7 (permalink)  
Old 05-31-06, 12:33 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
using chomp is generally safer than chop but it depends on what you are doing. If it's removing the newline (or record seperator) from the end then chomp should be used. chop will remove the last character of the string even if it's not a newline (or whatever $/ is set to).
Reply With Quote
  #8 (permalink)  
Old 06-05-06, 07:09 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks guys,

for your explanation.

I only want to have a online tool, like we have Notepad, Dreamweaver etc.; which we used to edit html files. instead of downloading and uploading files, i want to edit the file on the website it will look like same and do the same work as our edit software do.

But is case its putting extra spaces between lines.

Now using that Chomp, i am still observing the file. i needs some time to come into conclusion.

do you think this part have any problem:

Code:
sub get_results {

$subject = $query->param('subject');
chop($subject);

#### write the new entry
#-----------------------
open(DATAF,">$filename4") or die "Can't Open $filename4: $!\n";
print DATAF ($subject);
close(DATAF);
}
specially this part:
chop($subject);

should I use this??

Thanks.
Reply With Quote
  #9 (permalink)  
Old 06-05-06, 11:25 AM
curbview.com's Avatar
curbview.com curbview.com is offline
Junior Code Guru
 
Join Date: May 2006
Posts: 555
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sujata_ghosh

specially this part:
chop($subject);

should I use this??

Thanks.
That code has nothing to do with why you have extra line feeds in your file. Let me explain the code you just submitted:

$subject = 'I love sports';
chop($subject);

print "$subject";
Here is what $subject now looks like:

I love sport

If you read the previous posts, you will see why you still have extra lines in your data. Chop will always remove the last character in a string. Chomp, however, will remove the line break (if any).

Perl Manpage explains this:
chomp


This safer version of chop removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the English module). It returns the total number of characters removed from all its arguments. It's often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline. When in paragraph mode ($/ = "" ), it removes all trailing newlines from the string. When in slurp mode ($/ = undef ) or fixed-length record mode ($/ is a reference to an integer or the like, see perlvar) chomp() won't remove anything. If VARIABLE is omitted, it chomps $_ .

chop

Chops off the last character of a string and returns the character chopped. It is much more efficient than s/.$//s because it neither scans nor copies the string. If VARIABLE is omitted, chops $_ . If VARIABLE is a hash, it chops the hash's values, but not its keys.

You can actually chop anything that's an lvalue, including an assignment.

If you chop a list, each element is chopped. Only the value of the last chop is returned.

Note that chop returns the last character. To return all but the last character, use substr($string, 0, -1) .
Reply With Quote
  #10 (permalink)  
Old 06-06-06, 12:50 AM
sujata_ghosh sujata_ghosh is offline
Wannabe Coder
 
Join Date: May 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks curbview.com !

for the explanation, its help me a lot.

Now the problem is i have used chomp, but the problem its remove a single line also, and result of that i get a pile of code one by another with a line break.

How do i over come that? its need atleast one brake after a certain code. i have given those break into local editor, but at there, its just became a pile of code. do you have any solution for that if you want i can give you the URL of the file which we used to update with that CGI programe.

Thanks.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP code to edit a text file mdhall Script Requests 12 12-23-10 04:03 AM
Error message not getting displayed. sanjeet Windows .NET Programming 0 12-20-05 10:48 AM
Script for splitting a file into several files saleem Perl 4 06-27-05 08:55 PM
Script for picking the first HTML file out of a directory (with sorted HTML files) jelteveld Script Requests 0 08-16-03 01:30 PM


All times are GMT -5. The time now is 04:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.