9

Possible Duplicate:
PHP: Remote file size without downloading file

I want to find the size of a file from PHP when the user enters the link of the file which he uploaded on a different site. But how?

Community
  • 1
  • 1
Buffon
  • 373
  • 2
  • 4
  • 9
  • link from `mediafire` as an example how can i get the size – Buffon May 11 '11 at 02:53
  • before or after the file has been uploaded? – robx May 11 '11 at 02:54
  • @robx: the user enter any `mediafire` link for example and i give him the size of that file – Buffon May 11 '11 at 02:56
  • Unless the remote server gives you this information, you'll have to actually *download* the file first (or at least attempt to to get the headers that may tell you the size). So, *"for example"* is a bit too general, it really depends on individual cases whether it's possible or not. Are you *only* concerned about MediaFire, or *any* file *anywhere*? – deceze May 11 '11 at 03:02
  • If you're talking specifically about MediaFire, you can first grab the contents of the URL where the file is located, http://www.mediafire.com/?cic659qp2a2i71v, for example. Then, use a regular expression to find the file size on that page: ``. This regex will pull back the file size when applied to the source of the MediaFire page which hosts the file. – Josh M. May 11 '11 at 12:39

2 Answers2

17

I assume you want the size of a remote file. The following PHP code should do the trick:

<?php

    // URL to file (link)
    $file = 'http://example.com/file.zip';

    $ch = curl_init($file);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $data = curl_exec($ch);
    curl_close($ch);

    if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {

        // Contains file size in bytes
        $contentLength = (int)$matches[1];

    }
?>

You first need to obtain the URL to the file which, in relation to MediaFire and other file sharing websites, would need extra code to obtain after satisfying the free user waiting period.

wallyk
  • 56,922
  • 16
  • 83
  • 148
Steven Ross
  • 229
  • 1
  • 6
9
strlen(file_get_contents($url_file));

The strlen function is binary safe, so you just have to get the length of the file's contents.

Jim Rubenstein
  • 6,836
  • 4
  • 36
  • 54
  • 14
    What if `$url_file` points to 40Gb blueray remux? – zerkms May 11 '11 at 03:06
  • valid point zerkms. he should definitely check to make sure the URL is valid..I have another idea, though, i'll update my answer to be better (: – Jim Rubenstein May 11 '11 at 03:10
  • Or, instead, i'll upvote your comment above. – Jim Rubenstein May 11 '11 at 03:11
  • 1
    *valid*? What is *valid* url? Each time when the file is bigger than the memory limit - you'll get fatal error. – zerkms May 11 '11 at 03:11
  • @zerkms valid based on the constraints of his application. i was about to post an answer for checking filesize without downloading, then i saw that you linked it, so i just upvoted your comment. – Jim Rubenstein May 11 '11 at 03:15
  • I like this solution cause it doesn't need particular libs (curl) and it seems ok on small all-days images – nulll Mar 13 '13 at 16:23