Code Comments
Programming Forum and web based access to our favorite programming groups.I would like to be able to determine the size (in bytes) of a file and re-adjust before output. Is this possible? Norm -- Avatar hosting at www.easyavatar.com
Post Follow-up to this message"Norman Peelman" <npeelman@cfl.rr.com> wrote in message news:iqoOc.413$Hu2.378@tornado.tampabay.rr.com... > I would like to be able to determine the size (in bytes) of a file and > re-adjust before output. Is this possible? > > Norm > > -- > Avatar hosting at www.easyavatar.com > > No need to use GD, just use this: function my_filesize($file) { if(!is_file("./".$file)) return "0 KB"; $kb = 1024; // Kilobyte $mb = 1024 * $kb; // Megabyte $gb = 1024 * $mb; // Gigabyte $tb = 1024 * $gb; // Terabyte $size = filesize($file); if($size < $kb) { return $size." B"; } else if($size < $mb) { return round($size/$kb,2)." KB"; } else if($size < $gb) { return round($size/$mb,2)." MB"; } else if($size < $tb) { return round($size/$gb,2)." GB"; } else { return round($size/$tb,2)." TB"; } } Regards Richard Grove
Post Follow-up to this messageOn Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wro te: > I would like to be able to determine the size (in bytes) of a file and >re-adjust before output. Is this possible? If you mean determine the size of the file it _would_ create, you can't without encoding it to somewhere, either memory or a temporary file. You could capture output (ob_start()) etc., use imagepng/jpeg/whatever, and save the output buffer to a variable and use strlen to work out the size. Or save to a file anyway, and use filesize(), and regenerate it if you don't like the size. -- Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Post Follow-up to this message"Andy Hassall" <andy@andyh.co.uk> wrote in message news:ip3lg0lgo033sd54g7dnqv9crq857e2oo8@ 4ax.com... > On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wrote: > > > If you mean determine the size of the file it _would_ create, you can't > without encoding it to somewhere, either memory or a temporary file. > > You could capture output (ob_start()) etc., use imagepng/jpeg/whatever, and > save the output buffer to a variable and use strlen to work out the size. > > Or save to a file anyway, and use filesize(), and regenerate it if you don't > like the size. > > -- > Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space Thanks Andy, you understood what I meant... my mistake I should have said 'image' instead of 'file'. I'll look into the ob_* functions but wich do you think would be faster (or easier)? Norm -- Avatar hosting at www.easyavatar.com
Post Follow-up to this messageOn Fri, 30 Jul 2004 22:04:36 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wro te: >"Andy Hassall" <andy@andyh.co.uk> wrote in message > news:ip3lg0lgo033sd54g7dnqv9crq857e2oo8@ 4ax.com... >wrote: > >Thanks Andy, you understood what I meant... my mistake I should have said >'image' instead of 'file'. I'll look into the ob_* functions but wich do >you think would be faster (or easier)? I would guess ob_* would be faster, writing to a file would be easier, although I wouldn't have thought there a massive difference between the two. If you're writing out to a file in the end anyway, and more often than not y ou get reasonable settings the first time, I'd probably tend towards writing straight to a file. If you're outputting to the browser rather than saving to disk, I'd tend towards the ob_* solution. As ever, the only answer to what's best is It Depends (tm). -- Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Post Follow-up to this message"Andy Hassall" <andy@andyh.co.uk> wrote in message news:ip3lg0lgo033sd54g7dnqv9crq857e2oo8@ 4ax.com... > On Fri, 30 Jul 2004 09:21:18 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wrote: > > > If you mean determine the size of the file it _would_ create, you can't > without encoding it to somewhere, either memory or a temporary file. > > You could capture output (ob_start()) etc., use imagepng/jpeg/whatever, and > save the output buffer to a variable and use strlen to work out the size. > > Or save to a file anyway, and use filesize(), and regenerate it if you don't > like the size. > > -- > Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space Andy, Got it figured out with ob_start(), ob_get_length(), and ob_end_flush(). Thanks for the help! Norm -- Avatar hosting at www.easyavatar.com
Post Follow-up to this message"Andy Hassall" <andy@andyh.co.uk> wrote in message news:e2ilg0ln2jcumviea4q8kcj0mj9thjagnl@ 4ax.com... > On Fri, 30 Jul 2004 22:04:36 GMT, "Norman Peelman" <npeelman@cfl.rr.com> wrote: > <npeelman@cfl.rr.com> and can't imagepng/jpeg/whatever, size. > > I would guess ob_* would be faster, writing to a file would be easier, > although I wouldn't have thought there a massive difference between the two. > > If you're writing out to a file in the end anyway, and more often than not you > get reasonable settings the first time, I'd probably tend towards writing > straight to a file. > > If you're outputting to the browser rather than saving to disk, I'd tend > towards the ob_* solution. > > As ever, the only answer to what's best is It Depends (tm). > > -- > Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space I'm coming from a database (blob field) and going straight to browser (client) output and I didn't want to have to go to the filesystem. The ob_* funcs worked like a charm. -- Avatar hosting at www.easyavatar.com
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.