PDA

View Full Version : Perl upload progress bar


spetsacdc
07-06-2008, 05:10 AM
Hi, I know nothing about perl, but I think I need to make a progress bar for my ajax file upload.

Currently I have an ajax script set up that uses a php script to upload. This php script checks the file size and types to make sure it is safe to upload.

Now, is it possible to keep using my php upload script, but just add in a perl file that can send the info of how much of the file has already been uploaded?

It seems like all the scripts I have found actually upload via the perl script. It would be hard for me to rewrite my php upload script in perl.


Thanks

shyam
07-06-2008, 02:29 PM
since u already have a working php script and u know nothing of perl why not simply modify ur existing php script?

spetsacdc
07-06-2008, 04:00 PM
You have to like patch php, or use a pecl extension which I don't know if my free hosting can do. I prolly wont use perl b/c it seems like you have to use it to upload.

netroact
07-06-2008, 09:07 PM
PHP? Someone better buy this feller a drink!

spetsacdc
07-06-2008, 10:18 PM
I've never coded in perl before, but I am thinking of trying to use it for an upload script. The problem I encountered when using php and uploading to an iframe was that it would POST the entire 30mb file and then check the file size and say it was too big...

There was a perl/ajax upload script that had all the features I needed but one:

A file size limit for the folder being uploaded to. In php I looped through all files in the folder and added the sizes together.....

Are there any perl functions that can easily do this? I'm not experienced in perl at all...


Thanks

netroact
07-07-2008, 04:23 AM
I don't know what upload script you are using, but you should be able to add this code after you call the modules and before your upload code. Set the path to you directory, and set the total limit. You might want to change the error message. I've been watchin the western channel again on the satellite. If you don't know how to adapt this to your upload script, just post the script on here.




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

my $file_dir = "/home/user_name/public_html/directory_name";
my $limit = "5000";


opendir (COUNT, "$file_dir");
my @count_dir = readdir (COUNT);
closedir (COUNT);


foreach my $file_to_count (@count_dir)
{
$filesize = -s "$file_to_count";
$total = ($total + $filesize);
}

if ($total <= $limit)
{
$1;
}
else
{
print "Whoa Cowboy! You cain't stick that file in here!";
exit;
1;
}

spetsacdc
07-21-2008, 05:16 AM
Wow thanks! I'll save this for it I try to use perl again.

KevinADC
07-21-2008, 06:22 AM
What is $1 for in the code below?

if ($total <= $limit)
{
$1;
}

FishMonger
07-21-2008, 06:55 AM
First, since currently most if not all, browsers don't provide the file size in the upload header, you won't know the exact size until after you've uploaded the file. One common and probably the best option, for now, would be to upload to a temp directory x number of bytes at at time, and if/when it surpasses your limit, you stop the upload and output an appropriate error message. If it's within the limits, then you move it the final destination directory.

There are several issues with the netroact's example code.
1) It's missing proper error handling on the opendir command

2) Quoting single vars is considered a poor Perl programming practice and can introduce bugs/errors.
C:\>perldoc -q quoting
Found in C:\Perl\lib\pod\perlfaq4.pod
What's wrong with always quoting "$vars"?
The problem is that those double-quotes force stringification-- coercing
numbers and references into strings--even when you don't want them to be
strings. Think of it this way: double-quote expansion is used to produce
new strings. If you already have a string, why do you need more?

If you get used to writing odd things like these:

print "$var"; # BAD
$new = "$old"; # BAD
somefunc("$var"); # BAD

You'll be in trouble. Those should (in 99.8% of the cases) be the
simpler and more direct:

print $var;
$new = $old;
somefunc($var);

Otherwise, besides slowing you down, you're going to break code when the
thing in the scalar is actually neither a string nor a number, but a
reference:

func(\@array);
sub func {
my $aref = shift;
my $oref = "$aref"; # WRONG
}

You can also get into subtle problems on those few operations in Perl
that actually do care about the difference between a string and a
number, such as the magical "++" autoincrement operator or the syscall()
function.

Stringification also destroys arrays.

@lines = `command`;
print "@lines"; # WRONG - extra blanks
print @lines; # right


3) It's more efficient to process (loop over) the file list only once rather than creating the array and then looping over that array.

4) if ($total <= $limit)
{
$1;
}Do either of you know what this does and its purpose? Answer..It does nothing, servers no purpose, and could/would cause a warning.

5) foreach my $file_to_count (@count_dir)
{
$filesize = -s "$file_to_count";
$total = ($total + $filesize);
}

if ($total <= $limit)
{
$1;
}
else
{
print "Whoa Cowboy! You cain't stick that file in here!";
exit;
1;
}Reduces to:foreach my $file_to_count (@count_dir) {
$total += -s $file_to_count;

if ( $total > $limit ) {
print "Whoa Cowboy! You cain't stick that file in here!";
exit;
}
}

netroact
07-21-2008, 12:49 PM
Thanks for the replies. Don't know why you didn't reply when I orgiinally posted it. I usually use $1 to indicate true, and it does work, because I tested it first. I guess I didn't need it though.

The reason I used quotes is because in another script where I read the array into a variable, it broke the script if I didn't use quotes, and my feeble mind gets confused easily.

I guess I should have put the current upload file size in the mix, but at the time I didn't think that was what he was asking.

Thanks for the advice.

spetsacdc
07-21-2008, 04:10 PM
Thanks for the replies. Don't know why you didn't reply when I orgiinally posted it.


Sorry, I rely on email notifications and didn't get one for some reason.:)

netroact
07-22-2008, 12:18 AM
Sorry, I rely on email notifications and didn't get one for some reason.:)

Same thing happens to me sometimes. I don't get the instant notification every time.
Actually I was referring to FishMonger and KevenADC. I was expecting them to correct my code after my initial post. They usually help me out when I don't get it right.

KevinADC
07-22-2008, 01:12 AM
I never bothered to read this thread before.