Perl file directory functions

open(LOG, ">>../../../../logs/logfile.txt") || Error('open','file');

# to lock the file while writing to it, until closed
flock(LOG, 2) || Error('lock', 'file'); # this way two users don't simultaneously mess things up... flock(LOG,1) would request shared access to the file (for reading)

print LOG "$comments\n";

my @logmessages = <LOG>; # read the entire file into an array (each line an element)
while <LOG> {
#do such and such $_; } read the file line by line only
}
close(LOG) || Error('close','file');

print "Content-type: text/html\n\n";
print "<p>You said: <em>$comments</em>\n";
print qq(<hr>would you like to see all the <a href="http://www.cookwood.com/cgi-bin/perl2e/files/readfromlog.cgi">messages?</a>);

sub Error {
print "Content-type: text/html\n\n";
print "The Server can't $_[0] the $_[1]: $! \n";
exit;
}

# rename a file
# also interesting date approach...
# this can also be used to move a file, new location determined by second filename provided of course, if file already there it's overwritten, also, old location file is obliterated
my($mday,$mon,$year) = (localtime)[3,4,5];
$mon += 1;
$year += 1900;
my $date=sprintf('%04d%02d%02d', $year, $mon, $mday);
my $filename = "logfile$date.txt"
rename("../../../../logs/logfile.txt","../../../../logs/$filename") || Error('rename','file');

# remove a file
unlink("../../../../logs/$filename") || Error('delete','file');

# check if file: exists, readable, writable, executable, etc.
# -e = exists
# -r = permissions are such that file can be read
# -x permissions are such that file can be executed
# -w file can be written to
# -d check if it's a directory
if(-e "../../../../logs/$fileman") { print qq(A log does exist); } else { print "Sorry, that log file doesn't exist"; }

# access a directory:
opendir(LOGDIR, "../../../../logs/") || Error('open','directory');
my @logfiles = readdir(LOGDIR);
closedir(LOGDIR);
if(@logfiles) { print "<p>You can choose from the following logs:<ul>"; foreach(sort @logfiles){ print "<li>$_" if/^logfile/; } print '</ul>'; }

# change the working directory
chdir("../../../../logs") || Error('change','directory');

# create a new directory
mkdir("public/arhives",0777) || Error('make','directory');

# change file or directory permissions
if(-e "public/archives"){
chmod(0777, "public/archives") || Error('change','permissions');
# this line is a good example of outputting something, anything to the browser to avoid a visible no output error!
print "<p>permissions have been changed";
}

# to remove/delete a directory (that's empty! otherwise delete everything IN it FIRST...)
rmdir("../archives") || Error('remove','directory');
print "removed";


Posted on 28 Oct 2009, 20:20 - Category: Perl
Comments - Edit - Delete


Perl shebang -T explained / and file open example

Ever wonder what the -T in a Perl shebang line means?

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';

etc...

It keeps you from using untested outside data to modify other outside data...
It's Perl's built-in so-called tainted data tracking/limiting mechanism

If you're checking this from the command line:

perl -cT filename.cgi

Note:
Just like declaring a POST_MAX at the onset of a script, the -T will generate an Internal Server Error,

"The server encountered an internal error or misconfiguration and was unable to complete your request." blah blah.

Better to have built such detections angelically into your script...

To UNTAINT data and thus intentionally use outside data:

example:

$file =~ /^(comments\.txt|question\.txt)$/;
my $goodfilename = $1;

if($goodfilename) {
open (FILE, ">../../../../logs/public/$goodfilename") || Error('open', 'file for writing');
print FILE "$comments\n";
close FILE;
print "You wrote <em>$comments</em> to <strong>$file</strong>";
}
else {
print "Sorry, that filename is not allowed.";
}

in other words:
$outside_data =~ /regex/;
$clean_data = $1;
or
$clean_data = $outside_data =~ /regex/;

Posted on 18 Oct 2009, 21:21 - Category: Perl
Comments - Edit - Delete


Perl CGI.pm limit upload POST_MAX

either find your CGI.pm module itself, near the top, within initialize_globals subroutine, and change the $POST_MAX variable,

or:

at the top of your script change it, example:

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';
$CGI::POST_MAX = 50,000,000; (in bytes)

limit how much visitors can submit/upload via this script.

Note:
Just like declaring -T at the onset of a script, POST_MAX generate an Internal Server Error,

"The server encountered an internal error or misconfiguration and was unable to complete your request." blah blah.

Better to have built such detections angelically into your script...

Posted on 18 Oct 2009, 21:15 - Category: Perl
Comments - Edit - Delete


Perl CGI Security

http://www.w3.org/Security/Faq/wwwsf4.html

Posted on 18 Oct 2009, 20:47 - Category: Perl
Comments - Edit - Delete


Perl search string return index thereof

print index('fastener', 'e');

#result - counted from left and 0
4

Posted on 17 Oct 2009, 24:10 - Category: Perl
Comments - Edit - Delete


Pages: [1] [2] [3] [4] [5] ...
Loading