Home » Code, PHP Functions, Tutorials

PHP Get File Size of Remote File

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.

<form action="get_info.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>

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

<?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:
[b]File Size:[/b] 1024 bytes or 1 kilobytes.

0

Popularity: 19% [?]

Share/Bookmark this!

33 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on November 6, 2009 at 10:55 AM and filed under Code, PHP Functions, Tutorials category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors