我的脚本在localhost上运行正常。现在我已将我的所有图像移到另一个网站上,脚本将不再起作用。为什么会这样,我该如何解决?
错误:
警告:filesize(): C:\ xampp \ htdocs \ Projects \ MVC \ application \ class.security.php中的http://data.localsky.net/panel/img/blocked/box_main.gif的stat失败在线 15
我用以下函数调用该函数:
baseImg('http://data.localsky.net/panel/img/blocked/box_main.gif', false);
public function baseImg($path, $auto=true) {
$img_src = $path;
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);
if ( preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT']) ) {
if($auto) {
return '<img src="'.$img_scr.'" />';
} else {
echo $img_src;
}
} else {
if($auto) {
return '<img src="data:image/jpg;base64,'.$img_str.'" />';
} else {
return 'data:image/jpg;base64,'.$img_str;
}
}
}
答案 0 :(得分:2)
您不能在HTTP URL上使用filesize()。并非所有协议都提供调整数据或支持获取数据。 filesize()只能用于LOCAL文件。支持的协议列在函数的手册页中:http://php.net/filesize和http://php.net/manual/en/wrappers.http.php
答案 1 :(得分:2)
你可以试试这个
$imgbinary = file_get_contents($img_src);
答案 2 :(得分:0)
filesize只能用于本地文件。如果你想获得远程文件的文件大小,请使用以下代码:
<?php
$remoteFile = 'http://us.php.net/get/php-5.2.10.tar.bz2/from/this/mirror';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}
echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>
答案 3 :(得分:0)
正如MarcB指出的那样,你不能在远程文件上使用filesize()
。这是一个基于cURL的解决方案,我发现here:
<强>代码:强>
function retrieve_remote_file_size($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
return $size;
}
<强>用法:强>
echo retrieve_remote_file_size('http://data.localsky.net/panel/img/blocked/box_main.gif');
<强>输出:强>
53
希望这有帮助!