PHP Get File Size of Remote File

Previously in a post I made for uploading a file from 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.

We’ll do this through a simple form. By default a form will encode all characters and convert spaces to “+” so we will leave the enctype undefined.

Then we just need the text input named “url” which I gave a default value “Enter URL Here…” and the Javascript-related attribute – onfocus. So when you focus on the text input it will change from the default value to blank.

Finally of course, we include the submit input named “submit” with the value “Submit.” We will use this value to check that the  form was submitted.

[codesyntax lang=”html4strict” title=”HTML Form”]
<form action=”getinfo.php” method=”post”>
<input type=”text” name=”url” size=”40″ value=”Enter URL Here…” onfocus=”if(this.value == ‘Enter URL Here…’) this.value = ”;” /> <input type=”submit” name=”submit” value=”Submit” />
</form>
[/codesyntax]

This HTML form will submit the entered url to getinfo.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.

By default the function fgets() reads each line of a file 1024 bytes at a time. The function runs until either the specified length is reached (1024 bytes) or to the end of the file. It is more resource efficient to specify a read length:

“Until PHP 4.3.0, omitting it would assume 1024 as the line length. If the majority of the lines in the file are all larger than 8KB, it is more resource efficient for your script to specify the maximum line length.”php.net

getinfo.php

[codesyntax lang=”php” title=”getinfo.php Source Code”]
<?php
// getinfo.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.”;

}

}

?>
[/codesyntax]

The page will display the file size in bytes and kilobytes, as so:
File Size: 1024 bytes or 1 kilobytes.

Filed under: PHP, Tutorials, Web ProgrammingTagged with: , , ,

35 Comments

  1. this website is totally awesome.

  2. Great material – Thanks for posting that advice, I think that it basically answers my concern.

  3. Hmmm…great to find out, there were without a doubt two or three items which I hadn’t thought of before.

  4. I definitely knew about most of this, but even so, I still found it helpful. Great work!

  5. 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

  6. Super-Duper site! I am loving it!! Will come back again – taking your feeds also, Thank you.

  7. Thanks for publishing this, I found it to be really educational, and it answered almost all of the questions I had.

  8. I enjoyed reading your interesting yet very informative insights. I am looking forward to reading more of your most recent articles and blogs.

  9. Thank you for such a fantastic blog. Where else could one get this kind of info written in such an …

  10. This really is an excellent article, but I was questioning how do I suscribe for the RSS feed?

  11. 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.

  12. What a good blog you have here. Please update it more often. This topics is my interest. Thank you. . .

  13. Nice post and this mail helped me alot in my college assignement. Gratefulness you on your information.

  14. 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!

  15. 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.

  16. Excelentes Tips! a ponerlos en practica! Gracias!

  17. well I guess you learn something new everyday. Got something outta this that I realize before. Thanks…

  18. Maintain up the outstanding work mate. This weblog publish shows how well you comprehend and know this subject.

  19. This is really a marvelous source of data, Im pleased I read this article. I am going to be returning soon to look at more that you have.

  20. Great little tidbit of code. This could be very useful. Is there any particular reason why you are downloading only 1024 bits each time thru the loop?

    • I updated the post to show why, but it is more resource efficient to specify a reading length with PHP 4.3 and further, due to increased file size.

      So this value is dependent upon what type of file you are reading. Preferably a file with many line breaks.


Add a Comment

Your email address will not be published. Required fields are marked *

Comment *
Name *
Email *
Website