I have a script for uploading files to the web server. The code is as follows (I use cgi-lib.pl):
However, I want to upload only MS Word documents. So, how do I determine that the file being uploaded by a client is infact a MS Word document and nothing else.
Can I do that using cgi-lib.pl? I have been to the cgi-lib.pl website but did not find anything.
Also, if you notice, I am using a very crude way of overwriting the old file. I am first deleting the existing file and then uploading the new file. Can some improvement be made in that regard?
Thanks.
Code:
#!/usr/bin/perl
# Copyright (c) 1996 Steven E. Brenner
# $Id: fup.cgi,v 1.2 1996/03/30 01:35:32 brenner Exp $
use CGI::Carp qw(fatalsToBrowser);
require "./cgi-lib.pl";
$ref_number = 12345;
#Delete old file
$delfile = "/home/easy/public_html/cvs/$ref_number.doc";
unlink $delfile;
# When writing files, several options can be set... here we just set one
# Limit upload size to avoid using too much memory
$cgi_lib'maxdata = 50000;
$cgi_lib'writefiles = "/home/easy/public_html/cvs";
# Start off by reading and parsing the data. Save the return value.
# We could also save the file's name and content type, but we don't
# do that in this example.
$ret = &ReadParse;
# A bit of error checking never hurt anyone
&CgiDie("Error in reading and parsing of CGI input") if !defined $ret;
&CgiDie("No data uploaded", "Please enter it in <a href='fup.html'>fup.html</a>.")
if !$ret;
# Munge the uploaded text so that it doesn't contain HTML elements
# This munging isn't complete -- lots of illegal characters are left as-is.
# However, it takes care of the most common culprits.
$in{'upfile'} =~ s/</</g;
$in{'upfile'} =~ s/>/>/g;
$uploadedfile = $in{'upfile'};
rename($uploadedfile, "/home/easy/public_html/cvs/"."$ref_number".".doc");
# Now produce the result: an HTML page...
#print &PrintHeader;
print &HtmlTop("File Upload Results");
print <<EOT;
<p>You've uploaded a file $uploadedfile. Your notes on the file were:<br>
<blockquote>$in{'note'}</blockquote><br>
EOT
print &HtmlBot;
However, I want to upload only MS Word documents. So, how do I determine that the file being uploaded by a client is infact a MS Word document and nothing else.
Can I do that using cgi-lib.pl? I have been to the cgi-lib.pl website but did not find anything.
Also, if you notice, I am using a very crude way of overwriting the old file. I am first deleting the existing file and then uploading the new file. Can some improvement be made in that regard?
Thanks.