Previously in a post I made for uploading file with URL we wanted to include a file size check to make sure the file wasn’t too big before we upload it. Here I’ll show you how to get the remote file’s size and other information before doing other functions with it.
This HTML form will submit the entered url to get_info.php.
We are going to the function fopen() to get the information we need from the remote file. There is one thing to be aware of using fopen() to retrieve this data. You need to be sure the fopen wrappers is enabled in your php.ini. This can not be changed using ini_set().
We are going to create a variable “contents” and add each line of the file to it as we read it. Then we will use the function mb_strlen() to get the file size of the variable. Normally we would use strlen(), however that will only read one character as one byte. That doesn’t really give you the most accurate reading if you have special characters in the file which are more than one byte.
get_info.php
// get_info.php
if($_POST["submit"]){
// Form is submitted.
// Check the fopen wrapper settings in php.ini
if (ini_get("allow_url_fopen") == 1) {
// Open the file.
$file = fopen(trim($_POST["url"]), "r");
if($file){
// We got the file.
$contents = "";
while($line = fgets($file,1024)){
// Write each line to the string contents a kilobyte at a time.
$contents .= $line;
}
$filesize = mb_strlen($contents,"8bit");
$kb = $filesize / 1024; // Returns the file size in kilobytes
echo "<strong>File Size</strong>: ".$filesize." bytes or ".$kb." kilobytes.";
} else {
echo "Remote file not found.";
}
} else {
echo "Fopen wrappers not enabled.";
}
}
?>
The page will display the file size in bytes and kilobytes, as so:
File Size: 1024 bytes or 1 kilobytes.
Miranda on January 17, 2010
this website is totally awesome.
zaklady bukmacherskie on May 31, 2010
Great info, thanks for useful post. I’m waiting for more
Desmond on June 30, 2010
Great material – Thanks for posting that advice, I think that it basically answers my concern.
Defendor on July 3, 2010
Hmmm…great to find out, there were without a doubt two or three items which I hadn’t thought of before.
how download music on July 7, 2010
thank you for posting this one up..it is very interesting
airbender online on July 8, 2010
Great post. Hope to see much more great posts in the future.
Danae Lawin on July 9, 2010
I definitely knew about most of this, but even so, I still found it helpful. Great work!
backlinks on July 10, 2010
I just wanted to comment and say that I really enjoyed reading your blog post here. It was very informative and I also digg the way you write! Keep it up and I’ll be back to read more in the future
pinball download on July 12, 2010
Super-Duper site! I am loving it!! Will come back again – taking your feeds also, Thank you.
Chi on July 13, 2010
Thanks for publishing this, I found it to be really educational, and it answered almost all of the questions I had.
G.I. Joe full movie on July 16, 2010
Webmasters are under appreciated, thank you for taking the time to post this.
kampanie AdWords on July 18, 2010
Great text and nice site.
New York receptionist position on July 19, 2010
I can’t find how to subscribe to the comments via feedburner. I want to keep up to date on this, how do I do that?
PA Online Jobs on July 19, 2010
I couldn’t resist commenting.
Google Alternative Search Engine on July 23, 2010
I enjoyed reading your interesting yet very informative insights. I am looking forward to reading more of your most recent articles and blogs.
optimize pc on July 25, 2010
Thank you for such a fantastic blog. Where else could one get this kind of info written in such an …
California Gurls Full Video on July 26, 2010
I tried to subscribe to your rss feed, but had a problem adding it to google reader. Could you please check this out.
hughes net satellite internet on July 27, 2010
Here’s a comment. Great advice =) Thanks
Electric cigarette on July 27, 2010
This really is an excellent article, but I was questioning how do I suscribe for the RSS feed?
Revenge Of The Bridesmaids ABC on July 28, 2010
Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.
celebrity biography on July 28, 2010
What a good blog you have here. Please update it more often. This topics is my interest. Thank you. . .
asphalt roofing Wilmington de on July 31, 2010
Thank you for the advice. I’ve found your first point to be most effective.
richjanitor on August 7, 2010
Nice post and this mail helped me alot in my college assignement. Gratefulness you on your information.
Ricky Shead on August 10, 2010
Useful information, many thanks to the writer. It is puzzling to me now, but in general, the usefulness and importance is overwhelming. Very much thanks again and good luck!
Tambra Renkes on August 11, 2010
I absolutely enjoy reading your article, the form of writing is great.This blog as usual was instructive, I have had to bookmark your website and subscribe to your feed in ifeed. Your blog looks smashing.
Jake Ruston on August 11, 2010
Cheers for this post, it was great to read.
Dia de Spa on August 12, 2010
Excelentes Tips! a ponerlos en practica! Gracias!
Doradztwo finansowe on August 13, 2010
well I guess you learn something new everyday. Got something outta this that I realize before. Thanks…
Liberar Moviles on August 13, 2010
Excelentes Tips! a ponerlos en practica! Gracias!
Abril mayo on August 14, 2010
Interesting, many thanks
Kennedy harrison on August 14, 2010
Great, many thanks
Shanda Lamudio on August 20, 2010
Maintain up the outstanding work mate. This weblog publish shows how well you comprehend and know this subject.