Get remote file size from URL?

Possible duplicate:
PHP: remote file size without file upload

I want to find the file size from PHP when the user enters a link to the file that he uploaded to another site. But how?

+8
php
source share
2 answers

I assume you want the size of the deleted 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]; } ?> 

First you need to get the URL of the file, which in relation to MediaFire and other file sharing sites will require additional code to receive after satisfying the waiting period for a free user.

+16
source share
 strlen(file_get_contents($url_file)); 

The strlen function is binary security, so you just need to get the length of the file contents.

+10
source share

All Articles