Home Backend Development PHP Tutorial 3 solutions to get remote file size in PHP_PHP Tutorial

3 solutions to get remote file size in PHP_PHP Tutorial

Jul 21, 2016 pm 03:00 PM
file get p php code use copy big document method Obtain solve Remotely

1. Use file_get_contents()

Copy the code The code is as follows:

<?php
$file = file_get_contents($url);
echo strlen($file);
?>

2. Use get_headers()
Copy code The code is as follows:

<?php
$header_array = get_headers($url, true);
$size = $header_array['Content-Length'];
echo $size;
?>

PS:
required Open allow_url_fopen!
If it is not opened, it will be displayed
Warning: get_headers() [function.get-headers]: URL file-access is disabled in the server configuration
3. Use fsockopen ()
Copy code The code is as follows:

<?php
function get_file_size($url) {
$url = parse_url($url);

if (empty($url['host'])) {
return false;
}

$ url['port'] = empty($url['post']) ? 80 : $url['post'];
$url['path'] = empty($url['path']) ? '/' : $url['path'];

$fp = fsockopen($url['host'], $url['port'], $error);

if ($fp) {
                                                                                                                          host']. "rnrn");

while (!feof($fp)) {
$str = fgets($fp);
if (trim($str) == ' ') {
break;
}elseif(preg_match('/Content-Length:(.*)/si', $str, $arr)) {
return trim($arr[1]) ;
                                                                               ; 🎜> }
?>





http://www.bkjia.com/PHPjc/328048.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/328048.html

1. Use file_get_contents() to copy the code. The code is as follows: ?php $file = file_get_contents($url); echo strlen ($file); ? 2. Use get_headers() to copy the code. The code is as follows: ?php $header_...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles