Skip to main content

Get Remote File Size Using PHP

We can get a file's size by using PHP build in filesize function. But the problem is this function only works for local file. If you want to get a remote file's size you need to think some other way. In this post I am going to show you how we can get a remote file's size from it's header information with out downloading file. I will show you two example. One, using get_headers function and another using cURL. Using get_headers is very simple  approach  and works for every body. Using cURL is more advance and powerful. You can use any one of these. But I  recommend  to use cURL approach. Here we go..


get_headers  Approach:
/**
 * Get Remote File Size
 *
 * @param sting $url as remote file URL
 * @return int as file size in byte
 */
function remote_file_size($url){
	# Get all header information
	$data = get_headers($url, true);
	# Look up validity
	if (isset($data['Content-Length']))
		# Return file size
		return (int) $data['Content-Length'];
}

Usage:
echo remote_file_size('http://www.google.com/images/srpr/logo4w.png');

Output:
19978

cURL  Approach:
/**
 * Remote File Size Using cURL
 * @param srting $url
 * @return int || void
 */
function remotefileSize($url) {
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_NOBODY, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
	curl_exec($ch);
	$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
	curl_close($ch);
	if ($filesize) return $filesize;
}

Usages:
echo remotefileSize('http://www.google.com/images/srpr/logo4w.png');

Output:
19978

Comments

  1. Thanks for the script i'm sure this will come in helpful.

    ReplyDelete
  2. AWESOME!! thank you so much, already using it for fallback procedures.. :)

    ReplyDelete
  3. Thanks from France !

    ReplyDelete
  4. how to get file size of https://userscloud.com/zly0j447pruv

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training India. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  5. The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    animal jam | five nights at freddy's | hotmail login

    ReplyDelete
  6. "Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.php jobs in hyderabad.
    "

    ReplyDelete
  7. Yes, the article I was looking for. Your article gives me another approach on the subject. I hope to read more articles from you.
    wings io

    ReplyDelete
  8. Awesome writing. I enjoy reading of this article a lot. I love to do PSD to Cratejoy conversion in programming languages.

    ReplyDelete
  9. If we need to check size of file downloaded on user system. Then what we will do

    ReplyDelete
  10. kit tattoo A tattoo of an anchor on a sailor indicates they have sailed across the Atlantic.

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code. multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.

Live Comment System with jQuery Ajax PHP and MySQL

Recently I have been made a post that focused on how to submit form without page refreshing. That post shows the basic functionality of using jQuery Ajax. On the same follow this post will describe how we can create a comment system to post an instant comment without page refreshing. Let's start...

Resize Image While Uploading with PHP

On my previous post I was shown an example how to upload file with php and html. It was based on php simple image upload functionality. Today I am going to show you little bit advance of image uploading. Suppose if you run a website and want to allow users to upload image files than you need to think little bit about server's space and bandwidth. For this purpose you need to do some optimization with image files. Image resize while uploading is one of them. All major website do it in the same way. If a user uploads a 5mb image file they resize it in different sizes and store on there server. It helps them to seed up there website and reduce there budget. Now take a look, how we can do it ourself.