PHP通过Internet检查文件大小
如何通过Internet检查文件大小?下面的示例是我不工作的代码PHP通过Internet检查文件大小,php,Php,如何通过Internet检查文件大小?下面的示例是我不工作的代码 echo filesize('http://localhost/wordpress-3.1.2.zip'); echo filesize('http://www.wordpress.com/wordpress-3.1.2.zip'); 试试这个功能 <?php function remotefsize($url) { $sch = parse_url($url, PHP_URL_SCHEME);
echo filesize('http://localhost/wordpress-3.1.2.zip');
echo filesize('http://www.wordpress.com/wordpress-3.1.2.zip');
试试这个功能
<?php
function remotefsize($url) {
$sch = parse_url($url, PHP_URL_SCHEME);
if (($sch != "http") && ($sch != "https") && ($sch != "ftp") && ($sch != "ftps")) {
return false;
}
if (($sch == "http") || ($sch == "https")) {
$headers = get_headers($url, 1);
if ((!array_key_exists("Content-Length", $headers))) { return false; }
return $headers["Content-Length"];
}
if (($sch == "ftp") || ($sch == "ftps")) {
$server = parse_url($url, PHP_URL_HOST);
$port = parse_url($url, PHP_URL_PORT);
$path = parse_url($url, PHP_URL_PATH);
$user = parse_url($url, PHP_URL_USER);
$pass = parse_url($url, PHP_URL_PASS);
if ((!$server) || (!$path)) { return false; }
if (!$port) { $port = 21; }
if (!$user) { $user = "anonymous"; }
if (!$pass) { $pass = "phpos@"; }
switch ($sch) {
case "ftp":
$ftpid = ftp_connect($server, $port);
break;
case "ftps":
$ftpid = ftp_ssl_connect($server, $port);
break;
}
if (!$ftpid) { return false; }
$login = ftp_login($ftpid, $user, $pass);
if (!$login) { return false; }
$ftpsize = ftp_size($ftpid, $path);
ftp_close($ftpid);
if ($ftpsize == -1) { return false; }
return $ftpsize;
}
}
?>
我认为这可能是不可能的。最好的方法是通过file\u获取内容
下载文件,然后在文件上使用filesize
。您也可以稍后删除该文件 filesize函数用于获取本地存储的文件大小。*对于远程文件,您必须找到其他解决方案,例如:
<?php
function getSizeFile($url) {
if (substr($url,0,4)=='http') {
$x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) { $x = $x['content-length'][1]; }
else { $x = $x['content-length']; }
}
else { $x = @filesize($url); }
return $x;
}
?>
来源:见下面链接中的第一条帖子评论
*老实说,由于PHP 5中有一些文件函数的包装器,请参见此处:
你可以找到更多的例子,即使在这里,这应该满足你的需要:
下次提问前请尝试使用搜索功能可能的重复:您可以检查
内容长度
标题,但不能保证其准确性。最稳定的方法是将文件下载到临时位置的服务器上,然后在其上运行filesize()
。