#!/usr/bin/perl -w
# PHP File Uploader with progress bar Version 2.0
# Copyright (C) Raditha Dissanyake 2003
# http://www.raditha.com
# Changed for use with AJAX by Tomas Larsson
# http://tomas.epineer.se/
# Licence:
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under this License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Initial Developer of the Original Code is Raditha Dissanayake.
# Portions created by Raditha are Copyright (C) 2003
# Raditha Dissanayake. All Rights Reserved.
#
# CHANGES:
# As of version 1.00 cookies were abolished!
# as of version 1.02 stdin is no longer set to non blocking.
# 1.40 - POST is no longer required and processing is more efficient.
# Please refer online docs for details.
# 1.42 - The temporary locations were changed, to make it easier to
# clean up afterwards.
# 1.45.
# Changed the way in which the file list is passed to the php handler
# 2.0 (2006-03-12) (Tomas Larsson)
# Changed to work better with AJAX. This meant improved error handling
# and no forwarding to php page after successful upload. Also moved settings
# in to this file.
# 2.0 (2006-04-22) (Tomas Larsson)
# Settings are now read from a file above document root (shared with PHP code)
use CGI;
use Fcntl qw(:DEFAULT :flock);
use File::Temp qw/ tempfile tempdir /;
$docroot = $ENV{DOCUMENT_ROOT};
# Settings file should be placed one step above document root.
# If you can't place it there, change the row below to point to the actual location of the settings file
$settingsfile = "upload_settings.inc";
if (-e $settingsfile)
{
#Execute settingsfile (must be valid perl code)
do $settingsfile;
}
if(!(-e $settingsfile) or !$max_upload or !$tmp_dir)
{
#Default setting values
$max_upload = 5000000;
$tmp_dir="/tmp";
}
@qstring=split(/&/,$ENV{'QUERY_STRING'});
@p1 = split(/=/,$qstring[0]);
if("test" eq $p1[0]) {
print "Content-type: text/html\n\n";
print "Perl seems to be installed and working!
";
print "Settings
";
print "Max upload size: ".$max_upload."
";
print "Temp dir: ".$tmp_dir."
";
exit;
}
$sessionid = $p1[1];
$sessionid =~ s/[^a-zA-Z0-9]//g; # sanitized as suggested by Terrence Johnson.
$async_upload = 1;
$size = @qstring;
if($size == 2) {
@p2 = split(/=/,$qstring[1]);
$target_url = @p2[1];
$async_upload = 0;
}
# don't change the next few lines unless you have a very good reason to.
$post_data_file = "$tmp_dir/$sessionid"."_postdata";
$monitor_file = "$tmp_dir/$sessionid"."_flength";
$error_file = "$tmp_dir/$sessionid"."_err";
$signal_file = "$tmp_dir/$sessionid"."_signal";
$qstring_file = "$tmp_dir/$sessionid"."_qstring";
$len = $ENV{'CONTENT_LENGTH'};
$bRead=0;
$|=1;
sub bye_bye {
$mes = shift;
if($async_upload) {
# Try to open error file to output message too
$err_ok = open (ERRFILE,">", $error_file);
if($err_ok) {
print ERRFILE $mes; #write message to file, so can be read from fileprogress.php
close (ERRFILE);
} else {
# can't write error file. output alert directly to client
print "Content-type: text/html\n\n";
print "