Page 1 of 1

PHP Upload progress bar.... help needed

Posted: Mon Jun 22, 2009 11:46 am
by vedarthk
I am making my own small uploading website, I want uploading progress bar in it.
Since there is no php code for it (I donno). I got some CGI scripts but dont understand the
CGI script please can anyone suggest me something as I am using freehosting.

I have got w2box.4.1 which is a bit complex to understand.....
I want to know how the CGI file tracks the upload progress.... or there is some other way....

Re: PHP Upload progress bar.... help needed

Posted: Mon Jun 22, 2009 1:03 pm
by Eric!
Check in the tutorial section on this forum.

Re: PHP Upload progress bar.... help needed

Posted: Mon Jun 22, 2009 1:33 pm
by vedarthk
i cannot load the APC as i am using a freehosting service....

I am using l4rge.com...

Re: PHP Upload progress bar.... help needed

Posted: Mon Jun 22, 2009 5:31 pm
by Eric!
PHP can not access raw POST data (or at least very well), so without ACP, you need to use a different language (perl, J2EE, etc.)

Re: PHP Upload progress bar.... help needed

Posted: Tue Jun 23, 2009 8:19 am
by vedarthk
thankyou 'Eric'... can anyone help me with the CGI code i have got one....

Code: Select all

 
#############
# SETTINGS! #
#############
 
# set this to your tmp path, you can use this or a full path like the line afterward
$tmp_dir= "tmp"; 
#$tmp_dir= "/home/username/tmp";
#$tmp_dir= "~/tmp";
#$tmp_dir= "C:/wamp/tmp";
 
#############
# /SETTINGS #
#############
use CGI;
use Fcntl qw(:DEFAULT :flock);
use File::Temp qw/ tempfile tempdir /;
#use Carp;
$high_max_upload = 524288000; #it will be change by params
 
@qstring=split(/&/,$ENV{'QUERY_STRING'});
@p1 = split(/=/,$qstring[0]);
$sessionid = $p1[1];
$sessionid =~ s/[^a-zA-Z0-9]//g;  # sanitized as suggested by Terrence Johnson.
 
@p1 = split(/=/,$qstring[1]);
$max_upload = $p1[1];
$max_upload =~ s/[^0-9]//g;  # sanitized
if ($max_upload > $high_max_upload) {$max_upload = $high_max_upload;}
 
if (!$sessionid) {
    print "Content-type: text/html\n\n";
    if (-e $tmp_dir && -d $tmp_dir)
    { 
        print "CGI Working\n";
    } else
    {
        print "The tmp directory doesn't exist. Please correct upload.cgi!\n";
    }
    
    exit;
}
 
# 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";
 
#require("./header.cgi");
 
#carp "$post_data_file and $monitor_file";
 
$content_type = $ENV{'CONTENT_TYPE'};
$len = $ENV{'CONTENT_LENGTH'};
$bRead=0;
$|=1;
 
sub bye_bye {
    $mes = shift;
    $mes =~ s/'/\\'/g;
    # 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 "<script>alert('".$mes."');parent.location = '?error';</script>\n";
    }
    exit;
}
 
 
# see if we are within the allowed limit.
 
if($len > $max_upload)
{
    close (STDIN);
    bye_bye("The maximum upload size has been exceeded");
}
 
 
#
# The thing to watch out for is file locking. Only
# one thread may open a file for writing at any given time.
# 
 
if (-e "$post_data_file") {
    unlink("$post_data_file");
}
 
if (-e "$monitor_file") {
    unlink("$monitor_file");
}
 
 
sysopen(FH, $monitor_file, O_RDWR | O_CREAT)
    or &bye_bye ("Can't open numfile: $!");
 
# autoflush FH
$ofh = select(FH); $| = 1; select ($ofh);
flock(FH, LOCK_EX)
    or  &bye_bye ("Can't write-lock numfile: $!");
seek(FH, 0, 0)
    or &bye_bye ("Can't rewind numfile : $!");
print FH $len;  
close(FH);  
    
sleep(1);
 
 
open(TMP,">","$post_data_file") or &bye_bye ("Can't open temp file");
binmode TMP; 
 
#
# read and store the raw post data on a temporary file so that we can
# pass it though to a CGI instance later on.
#
 
 
 
my $i=0;
 
$ofh = select(TMP); $| = 1; select ($ofh);
            
while (read (STDIN ,$LINE, 4096) && $bRead < $len )
{
    $bRead += length $LINE;
    
    #select(undef, undef, undef,0.35);  # sleep for 0.35 of a second.
    
    # Many thanx to Patrick Knoell who came up with the optimized value for
    # the duration of the sleep
 
    $i++;
    print TMP $LINE;
}
 
close (TMP);
 
#
# We don't want to decode the post data ourselves. That's like
# reinventing the wheel. If we handle the post data with the perl
# CGI module that means the PHP script does not get access to the
# files, but there is a way around this.
#
# We can ask the CGI module to save the files, then we can pass
# these filenames to the PHP script. In other words instead of
# giving the raw post data (which contains the 'bodies' of the
# files), we just send a list of file names.
#
 
open(STDIN,"$post_data_file") or &bye_bye("Can't open temp file");
 
my $cg = new CGI();
my $qstring="";
my %vars = $cg->Vars;
my $j=0;
 
while(($key,$value) = each %vars)
{
    
    $file_upload = $cg->param($key);
 
    if(defined $value && $value ne '')
    {   
 
        my $fh = $cg->upload($key);
        if(defined $fh)
        {
            #carp $fh;
            ($tmp_fh, $tmp_filename) = tempfile(DIR => $tmp_dir);
            
            binmode $tmp_fh;
            while(<$fh>) {
                print $tmp_fh $_;
            }
 
            close($tmp_fh);
            chmod 0644, $tmp_filename;
            
            $fsize =(-s $fh);
 
            $fh =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
            $tmp_filename =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
            $qstring .= "file[name][$j]=$fh&file[size][$j]=$fsize&";
            $qstring .= "file[tmp_name][$j]=$tmp_filename&";
            $j++;
        }
        else
        {
            $value =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
            $qstring .= "$key=$value&" ;
        }
    }
}
 
open (QSTR,">", "$qstring_file") or &bye_bye ("Can't open output file");
print QSTR $qstring;
close (QSTR);
 
open (SIGNAL,">", $signal_file) or &bye_bye ("Can't open signal file");
print SIGNAL "\n";
close (SIGNAL);
 
print "Content-type: text/html\n\n";
print "<html><script>loc = parent.location.href.split('/');parent.location = loc.splice(0,loc.length-1).join('/') + '?sid=".$sessionid."';</script></html>";
 
END {
    #clean up the mess on abortion
    if (not -e $signal_file){
        if (-e $post_data_file){ unlink($post_data_file); }
        if (-e $monitor_file){ unlink($monitor_file); }
        if (-e $qstring_file){ unlink($qstring_file); }
        if (-e $tmp_filename){ unlink($tmp_filename); }
    }
}
 
i want to know what does this piece of code does and what output is given ????? :roll: