Созонов Алексей

Веб-разработчик

Узнать размер файла PHP

09.08.2015

Узнать размер файла по url, используя PHP:

get_headers

/**
 * 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'];
}

cURL

/**
 * 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;
}

Вывести размер фала в КБ, МБ, ГБ

#PHP