How to get number of uploaded bytes in php

phpasks

New Member
Messages
145
Reaction score
0
Points
0
Ok! I was looking through an AJAX upload script and what it did was open the temp file up and get its size.

PHP:
//Class to store read info
class ReadStatus{
    var $is_dir_error = 0;
    var $open_dir_error = 0;
    var $active = 0;
    var $uploaded_files = 0;
    var $bytes_uploaded = 0;
}

// Return the current size of the $_GET['temp_dir_sid'] - flength file size.
function GetBytesRead($temp_dir, $upload_id){
    $read_status = new ReadStatus;
    $temp_upload_dir = $temp_dir . $upload_id . '.dir';
    $flength_file = $temp_dir . $upload_id . '.dir/' . $upload_id . '.flength';

    if(is_dir($temp_upload_dir) && file_exists($flength_file)){
        if($handle = opendir($temp_upload_dir)){
            while(false !== ($file_name = readdir($handle))){
                if(($file_name != '.') && ($file_name != '..') && ($file_name != $flength_file)){
                    $read_status->bytes_uploaded += filesize($temp_upload_dir . '/' . $file_name);
                    $read_status->uploaded_files++;
                }
            }
            closedir($handle);

            $read_status->active = 1;

            if($read_status->uploaded_files > 0){ $read_status->uploaded_files -= 1; }
        }
        else{ $read_status->open_dir_error = 1; }
    }
    else{ $read_status->is_dir_error = 1; }

    return $read_status;
}
Taken from Uber Uploader (uber_get_progress.php).

This is very good example for read image uploading bytes.

You can use this example - I am preferring to you.
 

oscetips

New Member
Messages
26
Reaction score
0
Points
0
reading the uploaded bytes is not a big problem in my opinion. but its the ability to be able to access a file to read which is the issue!
as i previously said, PHP does not allow access to the data whilst uploading, so you need to use Perl/CGI..

To do this - create your basic html upload form. and also create an invisible iframe. post the form to the iframe.

this is my upload.cgi script stripped to basics:

Code:
#!/usr/bin/perl -w

use CGI;
use Fcntl qw(:DEFAULT :flock);
use CGI::Carp qw/fatalsToBrowser/;

$len = $ENV{'CONTENT_LENGTH'};
open(FS,">../uploading/len.txt");
print FS $len;
close FS;

$query = new CGI;


$upload_dir = "../videos/".$query->param("id");
$filename = $query->param("vidfile");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("vidfile");

print "Content-type: text/html\n\n";
print "uploading...";



open(UPLOADFILE, ">$upload_dir/$filename") or die "Can't open '$upload_dir/$filename': $!";
binmode UPLOADFILE;
$|=1;


while (read ($upload_filehandle ,$LINE, 4096) && $bRead < $len ){
$bRead += length $LINE;

select(undef, undef, undef,0.35); # sleep for 0.35 of a second.

$i++;
print UPLOADFILE $LINE;
}


close UPLOADFILE;

print "done!";

len.txt will hold the total size of your upload. and the sizeof "$upload_dir/$filename" tells you your progress .....

this script works for me except thers a huuge delay before uploading occurs... can anyone shed some light on this please?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Good catch. Sadly, APC doesn't seem to be installed on the free hosts.
 
Top