i've been googleing but all i get is getimagesize and filesize.
getimagesize dosent get the KB size just width and height which is not what im looking for.
filesize give me the message Warning: filesize() [function.filesize]: stat failed for
the file in question is 51kb .jpg file
$imgsize=filesize("http://localhost/projects/site/schwe/user/1/1.jpg");
does not work,
how do i accomplish this?
You cannot get file size of remote elements, either give a relative path on your system OR do a file_get_contents() to get contents first
. Thus, instead of http:// , do a filesize('/path/to/local/system') . Make sure its readable by php process
You can't look up the filesize of a remote file like that. It is meant for looking at the filesize of local files.
For instance...
$imgsize = filesize( '/home/projects/site/1.jpg' );
filesize() is the function to use. It might be failing because
You're trying to get a web address & URL wrappers may not be turned on
That URL isn't valid.
If you're trying to run filesize() on a local file, reference the file system path, not some web URL.
Or you can also do something like :
$header = get_headers($url);
foreach ($header as $head) {
if (preg_match('/Content-Length: /', $head)) {
$size = substr($head, 15);
}
}
filesize takes the name of the file as argument not a URL and it returns the size of the file in bytes. You can divide the return value with 1024 to get the size in KB.
I had the same problem, which i solved like this. I don't know how optimal it is, but it works for me:
getimagesize("http://localhost/projects/site/schwe/user/1/1.jpg");
$file_size = $file[0]*$file[1]*$file["bits"];
Related
The following code is part of a PHP web-service I've written. It takes some uploaded Base64 data, decodes it, and appends it to a file. This all works fine.
The problem is that when I read the file size after the append operation I get the size the file was before the append operation.
$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);
$newSize = filesize($filepath.$filename); // gives old file size
What am I doing wrong?
System is:
PHP 5.2.14
Apache 2.2.16
Linux kernel 2.6.18
On Linux based systems, data fetched by filesize() is "statcached".
Try calling clearstatcache(); before the filesize call.
According to the PHP manual:
The results of this function are
cached. See clearstatcache() for more
details.
http://us2.php.net/manual/en/function.filesize.php
Basically, you have to clear the stat cache after the file operation:
$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);
clearstatcache();
$newSize = filesize($filepath.$filename);
PHP stores all file metadata it reads in a cache, so it's likely that the file size is already stored in that cache, and you need to clear it. See clearstatcache and call it before you call filesize.
I have a php script that needs to determine the size of a file on the file system after being manipulated by a separate php script.
For example, there exists a zip file that has a fixed size but gets an additional file of unknown size inserted into it based on the user that tries to access it. So the page that's serving the file is something like getfile.php?userid=1234.
So far, I know this:
filesize('getfile.php'); //returns the actual file size of the php file, not the result of script execution
readfile('getfile.php'); //same as filesize()
filesize('getfile.php?userid=1234'); //returns false, as it can't find the file matching the name with GET vars attached
readfile('getfile.php?userid=1234'); //same as filesize()
Is there a way to read the result size of the php script instead of just the php file itself?
filesize
As of PHP 5.0.0, this function can also be used with some URL
wrappers.
something like
filesize('http://localhost/getfile.php?userid=1234');
should be enough
Someone had posted an option for using curl to do this but removed their answer after a downvote. Too bad, because it's the one way I've gotten this to work. So here's their answer that worked for me:
$ch = curl_init('http://localhost/getfile.php?userid=1234');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //This was not part of the poster's answer, but I needed to add it to prevent the file being read from outputting with the requesting script
curl_exec($ch);
$size = 0;
if(!curl_errno($ch))
{
$info = curl_getinfo($ch);
$size = $info['size_download'];
}
curl_close($ch);
echo $size;
The only way to get the size of the output is to run it and then look. Depending on the script the result might differ though for practical use the best thing to do is to estimate basd on your knowledge. i.e. if you have a 5MB file and add another 5k user specific content it's still about 5MB in the end etc.
To expand on Ivan's answer:
Your string is 'getfile.php' with or without GET parameters, this is being treated as a local file, and therefore retrieving the filesize of the php file itself.
It is being treated as a local file because it isn't starting with the http protocol. See http://us1.php.net/manual/en/wrappers.php for supported protocols.
When using filesize() I got a warning:
Warning: filesize() [function.filesize]: stat failed for ...link... in ..file... on line 233
Instead of filesize() I found two working options to replace it:
1)
$headers = get_headers($pdfULR, 1);
$fileSize = $headers['Content-Length'];
echo $fileSize;
2)
echo strlen(file_get_contents($pdfULR));
Now it's working fine.
I have an interesting problem. I need to do a progress bar from an asycronusly php file downloading. I thought the best way to do it is before the download starts the script is making a txt file which is including the file name and the original file size as well.
Now we have an ajax function which calling a php script what is intended to check the local file size. I have 2 main problems.
files are bigger then 2GB so filesize() function is out of business
i tried to find a different way to determine the local file size like this:
.
function getSize($filename) {
$a = fopen($filename, 'r');
fseek($a, 0, SEEK_END);
$filesize = ftell($a);
fclose($a);
return $filesize;
}
Unfortunately the second way giving me a tons of error assuming that i cannot open a file which is currently downloading.
Is there any way i can check a size of a file which is currently downloading and the file size will be bigger then 2 GB?
Any help is greatly appreciated.
I found the solution by using an exec() function:
exec("ls -s -k /path/to/your/file/".$file_name,$out);
Just change your OS and PHP to support 64 bit computing. and you can still use filesize().
From filesize() manual:
Return Values
Returns the size of the file in bytes, or FALSE (and generates an
error of level E_WARNING) in case of an error.
Note: Because PHP's integer type is signed and many platforms use
32bit integers, some filesystem functions may return unexpected
results for files which are larger than 2GB.
The following code is part of a PHP web-service I've written. It takes some uploaded Base64 data, decodes it, and appends it to a file. This all works fine.
The problem is that when I read the file size after the append operation I get the size the file was before the append operation.
$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);
$newSize = filesize($filepath.$filename); // gives old file size
What am I doing wrong?
System is:
PHP 5.2.14
Apache 2.2.16
Linux kernel 2.6.18
On Linux based systems, data fetched by filesize() is "statcached".
Try calling clearstatcache(); before the filesize call.
According to the PHP manual:
The results of this function are
cached. See clearstatcache() for more
details.
http://us2.php.net/manual/en/function.filesize.php
Basically, you have to clear the stat cache after the file operation:
$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);
clearstatcache();
$newSize = filesize($filepath.$filename);
PHP stores all file metadata it reads in a cache, so it's likely that the file size is already stored in that cache, and you need to clear it. See clearstatcache and call it before you call filesize.
Currently if a user POST/uploads a photo to my PHP script I start out with some code like this
getimagesize($_FILES['picture1']['tmp_name']);
I then do a LOT more stuff to it but I am trying to also be able to get a photo from a URL and process it with my other existing code if I can. SO I am wanting to know, I f I use something like this
$image = ImageCreateFromString(file_get_contents($url));
Would I be able to then run getimagesize() on my $image variable?
UPDATE
I just tried this...
$url = 'http://a0.twimg.com/a/1262802780/images/twitter_logo_header.png';
$image = imagecreatefromstring(file_get_contents($url));
$imageinfo = getimagesize($image);
print_r($imageinfo);
But it didnt work, gave this.
Warning: getimagesize(Resource id #4) [function.getimagesize]: failed to open stream: No such file or directory in
Any idea how I can do this or something similar to get the result I am after?
I suggest you follow this approach:
// if you need the image type
$type = exif_imagetype($url);
// if you need the image mime type
$type = image_type_to_mime_type(exif_imagetype($url));
// if you need the image extension associated with the mime type
$type = image_type_to_extension(exif_imagetype($url));
// if you don't care about the image type ignore all the above code
$image = ImageCreateFromString(file_get_contents($url));
echo ImageSX($image); // width
echo ImageSY($image); // height
Using exif_imagetype() is a lot faster than getimagesize(), the same goes for ImageSX() / ImageSY(), plus they don't return arrays and can also return the correct image dimension after the image has been resized or cropped for instance.
Also, using getimagesize() on URLs isn't good because it'll consume much more bandwidth than the alternative exif_imagetype(), from the PHP Manual:
When a correct signature is found, the
appropriate constant value will be
returned otherwise the return value is
FALSE. The return value is the same
value that getimagesize() returns in
index 2 but exif_imagetype() is much
faster.
That's because exif_imagetype() will only read the first few bytes of data.
If you've already got an image resource, you'd get the size using the imagesx and imagesy functions.
getimagesize can be used with HTTP.
Filename - It can reference a local file or (configuration permitting) a remote file using one of the supported streams.
Thus
$info = getimagesize($url);
$image = ImageCreateFromString(file_get_contents($url));
should be fine.
Not sure if this will help, but I ran into a similar issue and it turned out the firewall controlled by my host was blocking outgoing http connection from my server.
They changed the firewall settings. My code then worked.
BTW: I thought this might have been an issue when I tried file_get_contents() on a number of urls, none of which worked!