开发者

Get size of remote file from URL? [duplicate]

开发者 https://www.devze.com 2023-03-04 20:01 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: PHP: Remote file size with开发者_JAVA技巧out downloading file
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

PHP: Remote file size with开发者_JAVA技巧out 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消