A sample Perl CGI program that can be used to edit files on a web site

Below I've included a sample Perl CGI program that I use to edit some files on my web sites. I've modified the file a little bit for the purposes of this example, but it's essentially what I use.

Very Important: This program by itself is not secure in any way. It does not require a user login, etc. At the very least you will want to secure access to this program with an Apache htaccess configuration, or something similar on other Perl CGI web servers. This program is shown here for demonstration purposes only.

As an end user of this program, I basically hit the URL that corresponds to the location of this CGI program. The Perl CGI program first displays the contents of the desired file in a textarea, and lets me make changes to the file within that textarea. I can edit the contents of the file, then press Save, and it writes my changes back to the file. Simple, quick, efficient.

The biggest change I've made to this example is to hard-code the path to the data file that will be edited. A more flexible version of this program would leave the file path as a variable, letting you edit any file on your web site ... but that doesn't always strike me as a really safe thing to do, and I especially don't want to encourage something like that in a sample program like this.

Without any more delay here is the sample file. Like anything else I know if can be improved, but hey, it works fine for what I need.

#!/usr/bin/perl
#
# created by devdaily.com, for demonstration purposes only.
#
# note: this program is not secure, and must be secured by apache htaccess
#       or other security mechanisms.
#

$FILE = '/home/apache/htdocs/blog/blog.rss';

#------------------------<<  htmlDie  >>-----------------------#
#
#  print an error message to stdout, then exit
#
#--------------------------------------------------------------#

sub htmlDie 
{
  $msg = shift;
  print $msg;
  exit;
}

#-------------------<<  writeOutputFile  >>--------------------#
#
#  USAGE: writeOutputFile $filename @whatToWrite
#
#--------------------------------------------------------------#

sub writeOutputFile 
{
  my $theFilename = shift;
  my $theContents = shift;

  open (OUT, "> $theFilename") || \
    &htmlDie ("Error: Could not open output file!\n");

  my @contents = split '\n', $theContents;
  my $line;
  foreach $line (@contents)
  {
    chomp($line);
    next if ( $line =~ /^\s*$/m );
    print OUT "$line\n";
  }
  close (OUT);
}


#---------------------<<  readContents  >>--------------------#
#
#  USAGE:  @contents = &readContents(filename);
#
#--------------------------------------------------------------#

sub readContents
{
  my $fullFilename = shift;
  my @contents;
  open(FILE,$fullFilename);
  while (<FILE>)
  {
    push @contents, $_;
  }
  close(FILE);
  return @contents;
}
  
#---------------------<<  printEditForm  >>--------------------#
#
#  USAGE:  &printEditForm($header,@contents);
#
#--------------------------------------------------------------#

sub printEditForm
{
  my ($header,@contents) = @_;
  my $theContents;
  foreach $line (@contents)
  {
    next if ( $line =~ /^\s*$/m );
    $theContents = $theContents . $line;
  }
  print $query->h3($header);
  print $query->startform;
  print $query->hidden('action', "save");
  print $query->textarea(-name=>'TEXT_AREA',
        -default=>$theContents,
        -rows=>30,
        -columns=>80);
  print $query->br;
  print $query->submit(-value=>'Save Changes');
  print $query->endform;
  print $query->end_html;
}


#------------------------------#
#  1. Create a new CGI object  #
#------------------------------#

use CGI;
$query = new CGI;


#----------------------------------#
#  2. Print the doctype statement  #
#----------------------------------#

print $query->header;


#----------------------------------------------------#
#  3. Start the HTML doc, and give the page a title  #
#----------------------------------------------------#

print $query->start_html('RSS File Editor');


#---------------#
#  4. Do it. :) #
#---------------#

my $action = $query->param('action');
my $textAreaContents = $query->param('TEXT_AREA');

if ( $action eq "" )
{
  $fullFilename = $FILE;
  my @contents = &readContents($fullFilename);
  &printEditForm("Editing the RSS File",@contents);
  exit;
}
elsif ( $action eq "save" )
{
  my $fullFilename = $FILE;
  &writeOutputFile($fullFilename,$textAreaContents);
  my $header = "Editing File:  $FILE";
  $header = $header . "
(file saved)";
  my @contents = &readContents($fullFilename);
  &printEditForm($header,@contents);
  exit;
}

Note that you will need the Perl CGI module for this application.

Comments

Permalink

Hi,

I tried the above code.but its not working , don know where is the error.if i edit some thing in the Text Area and the press save changes nothing is effecting in the file which i have been hard coded or else if i simple press the save changes says "file could not open". it would be great if i overcome this issue.

Thanks!
Ashwini

It sounds like that may be a permissions problem ... sorry, I see I didn't cover that in my original article.

If you're running this under Apache, the Apache httpd processes on a Linux system typically run as the user nobody, so the file you're reading-from and writing-to needs to have write permission for that user. (I *think* you'll find these errors in the Apache error log, but I'm not positive.)

To change the permission for the file shown in my example, I'd log in as the root user, and issue two commands like this on a Linux system:

chown nobody.nobody /home/apache/htdocs/blog/blog.rss
chmod 644 /home/apache/htdocs/blog/blog.rss

You can change that second command to this if you prefer:

chmod 664 /home/apache/htdocs/blog/blog.rss

Making it 664 instead of 644 makes the file writable by the group nobody. I don't think that's necessary in this case, but some people seem to prefer that.

Permalink

Hi alvin,

I dont find the path /home/Apache/htdocs/blog/blog.rss
in my Linux machine,so i saved a file in the /root/blog.rrs and hardcoded the path of the file in the code as you did ,gave the permission as you mentioned in the reply . I tried saving the file with some other extention like txt,html.But i don find any kind of progress

just a couple of doubts.Can we edit the txt or html files and save in the /var/www/cgi-bin/ location as well save the file as link in the webpage so as to open the saved files in the webpage and edit ,save it with different file name.

Please help me out :)

It sounds like you might want something more like a full-blown file management program? If so, take a look at this URL: 

http://cgi.resourceindex.com/Programs_and_Scripts/Perl/File_Management/

They have a nice listing of free file management programs.

The script I showed in this article was really just for editing the contents of one file, where the full path to that file was hard-coded in the script.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.