Issue to determine a currently downloading file size? - php

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.

Related

Download the first 5kb of a file with PHP as plain text?

Let's say I'd like to download some information from a file on the internet within PHP, but I do not need the entire file. Therefore, loading the full file through
$my_file = file_get_contents("https://www.webpage.com/".$filename);
would use up more memory and resources than necessary.
Is there a way to download only e.g. the first 5kb of a file as plain text with PHP?
EDIT:
In the comments it was suggested to use e.g. maxlen arg for file_get_contents or similar. But what I noticed that the execution time of the call does not vary appreciably for different maxlen which means that the function loads the full file and then just returns a substring to the variable.
Is there a way to make PHP download just the required amount of bytes and no more, to speed things up?
<?php
$fp = fopen("https://www.webpage.com/".$filename, "r");
$content = fread($fp,5*1024);
fclose($fp);
?>
Note: Make sure allow_url_fopen is enabled.
PHP Doc: fopen, fread

Why does SplFileInfo return a wrong size [duplicate]

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.

PHP filesize of dynamically chosen file

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.

php get the KB size of an image

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"];

PHP filesize reporting old size

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.

Categories