473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Get File size

15
THe script I want to modify shows file size like this
Expand|Select|Wrap|Line Numbers
  1. $size = fsize($target_path);
  2.  
If file is more then 1024kb it shows size in MB, if it is less then 1024kb it shows size in KB. Is it possible with IF-ELSE function to show size only in kb?
Aug 25 '10 #1

✓ answered by TheServant

This should give you everything in kB:
Expand|Select|Wrap|Line Numbers
  1. function fsize($file) {
  2.         $size = filesize($file);
  3.         $kbsize = $size / 1024;
  4.         return round($kbsize,2)." kB"; 
  5. }

3 4603
TheServant
1,168 Expert 1GB
That is a custom function, not a PHP function. The PHP function is: filesize() which returns the value in bytes. Simply divide that by 1024 to get kilobytes. You will also need to add your own units.

If you want to use a better custom function, have a look here where a second parameter can be put in to say what unit you wanted the size in.
Aug 25 '10 #2
dada1
15
Thanks, is this it?
Expand|Select|Wrap|Line Numbers
  1. function fsize($file) {
  2.         $a = array("B", "KB", "MB", "GB", "TB", "PB");
  3.         $pos = 0;
  4.         $size = filesize($file);
  5.         while ($size >= 1024) {
  6.                 $size /= 1024;
  7.                 $pos++;
  8.         }
  9.  
  10.         return round($size,2)." ".$a[$pos];
  11. }
How to modify that
Aug 25 '10 #3
TheServant
1,168 Expert 1GB
This should give you everything in kB:
Expand|Select|Wrap|Line Numbers
  1. function fsize($file) {
  2.         $size = filesize($file);
  3.         $kbsize = $size / 1024;
  4.         return round($kbsize,2)." kB"; 
  5. }
Aug 25 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Joe | last post by:
Hello, I have IIS 6.0 running on w2k3 Enterprise. I have three upload forms 1 to have restricted uploads .asp 2 to upload plain .asp any extension 3 a FP form upload non .asp
17
by: Arnold | last post by:
Is using fseek and ftell a reliable method of getting the file size on a binary file? I thought I remember reading somewhere it wasn't... If not what would be the "right" and portable method to...
6
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Hello all, I recall several threads over the years about how reading file size cannot be done consistantly or portably, but I...
49
by: Sam | last post by:
Hi all, Is there a function in the standard library that can get the size of a file? Thank you very much. Sam.
5
by: Claudio Grondi | last post by:
I have just started to play around with the bsddb3 module interfacing the Berkeley Database. Beside the intended database file databaseFile.bdb I see in same directory also the __db.001...
11
by: Parrot | last post by:
Is there any routine I can call to reduce the size of an image file after uploading a file from a client. I am looking to reduce file sizes programmatically using C# in my web page after uploading....
5
by: Jefferis NoSpamme | last post by:
Hi all, I'm trying to limit the file size of an image submission and I keep running into various problems. I've got most of it working, but I'm stumped and I have a basic question as to WHY this...
7
by: Adrián E. Córdoba | last post by:
Hi, there! I'm developing a little application which must record some events in a log file (.txt). How can I delete the first line in the log file every time I add a new line at the end, in order...
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.