Checking filesize

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cnapsys
Forum Newbie
Posts: 15
Joined: Tue Nov 14, 2006 5:57 pm

Checking filesize

Post by cnapsys »

Hi guys,
I'm having trouble building this script.
I'm trying to check the file size of the temp file multiple times.
Here's what I've tried so far:

Code: Select all

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path))
	{
	for($i=0; $i<=10; $i++)
		{
		$cksize = filesize($_FILES['userfile']['tmp_name']);
		echo '<br>'.$cksize;
                                }
		echo "<br>The file has been uploaded";
	}
the output tells me that the file has already been moved and it can't get the filesize().
I'm not even sure this can be done. Maybe I didn't understand the way move_uploaded_files() works.
If I can get multiple values of the file size while uploading I could do some nifty little things ;)

Thanks for the tip on including the

Code: Select all

code.
Last edited by cnapsys on Tue Nov 14, 2006 6:13 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The file has finished uploading by the time the script is called. move_uploaded_file() moves the file. If you try to get the filesize of the original location, it will fail if you've already moved the file.

$_FILES['foo']['size'] contains the file size, just so you know.
cnapsys
Forum Newbie
Posts: 15
Joined: Tue Nov 14, 2006 5:57 pm

Post by cnapsys »

Hi feyd,
yeah I know how to get the initial filesize().
What I'm trying to do is get several values of the filesize of the file that is being written.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Being written my move_uploaded_file()? I'm pretty sure it is done before you get control back from the function.
cnapsys
Forum Newbie
Posts: 15
Joined: Tue Nov 14, 2006 5:57 pm

Post by cnapsys »

would there be another way (maybe using a different function)??? to get those intermediate values????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unless you write the file yourself, which won't work on safe_mode enabled servers, no.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

I think you're trying to build an upload progress bar: http://www.google.com/search?q=php%20upload%20progress
cnapsys
Forum Newbie
Posts: 15
Joined: Tue Nov 14, 2006 5:57 pm

Post by cnapsys »

Ambush Commander wrote:I think you're trying to build an upload progress bar: http://www.google.com/search?q=php%20upload%20progress
I think you're right :D
however, I was trying to do it somewhat different from what's out there.
At this point anything would be helpful.
iframes/javascript/ anything :)

I give up :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The way you're trying to do it, the file has already finished uploading in its entirety.
cnapsys
Forum Newbie
Posts: 15
Joined: Tue Nov 14, 2006 5:57 pm

Post by cnapsys »

feyd wrote:The way you're trying to do it, the file has already finished uploading in its entirety.
trust me feyd. I got that from your first reply regarding the move_uploaded_file :)
I was just trying some work arounds and then I tried the curent progress meters found on google, but I'm not happy with any of 'em.
It looks like 5.2 may have a nifty little feature based on YUI: http://progphp.com/progress.phps
however we can't consider upgrading the php vs on the production server at this point just for this feature.
I'm still on the look out for a work around with iframes.

thanks for your help guys anyway
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

cnapsys wrote:It looks like 5.2 may have a nifty little feature based on YUI: http://progphp.com/progress.phps
please excuse my blindness but what is the nifty 5.2 feature there?
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

move_uploaded_file() returns the control only after whole file is being moved and PHP don't have that feaure which allows us to display progress bar while file is being uploaded.
cnapsys
Forum Newbie
Posts: 15
Joined: Tue Nov 14, 2006 5:57 pm

Post by cnapsys »

volka wrote:
cnapsys wrote:It looks like 5.2 may have a nifty little feature based on YUI: http://progphp.com/progress.phps
please excuse my blindness but what is the nifty 5.2 feature there?
the nifty little feature in 5.2 is:
php_rfc1867_callback

From Zend Mailing lists:
"php_rfc1867_callback - a callback function called on the following events:
- MULTIPART_EVENT_START
- MULTIPART_EVENT_FORMDATA
- MULTIPART_EVENT_FILE
- MULTIPART_EVENT_UPDATE
- MULTIPART_EVENT_END"

the link I posted is to an example by Rasmus Lerdorf:
http://progphp.com/progress.php

the 's' at the end of the link takes you to the source page.

It seems that you need to have 'apc.rfc1867 = on' in php.ini for this to work.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Mentioned here too: viewtopic.php?t=58234

But unless you're going to do some PHP extension hacking it's not much use.
Post Reply