如何判断一个数据是不是图片 是这样的,我需要获取远程的图片,然后写入本地但是有时候会出错误,比如获取了一个404之类的现在 $str 是获取到的数据那么如何判断 $str 是一个图片呢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php$filename='filename';file_put_contents($filename,$str);if($imginfo = @getimagesize($filename)){ //以下进行你要的处理 echo "{$filename}是一张图片";}else{ //以下进行不是图片文件的处理 echo "{$filename}不是图片文件"; @unlink($filename);} 404错误是没有找到你请求的资源。在确保请求的图片资源存在的前提下,可以通过判断获取http请求数据的contentType来判断其是否为图片,或者其它资源。当contentType包含image关键字的时候,就说明该资源是一个图片资源。<?phpfunction contains($str, $content, $ignorecase=true){ if ($ignorecase) { $str = strtolower($str); $content = strtolower($content); } return strpos($content,$str) ? true : false;}$url='http://yourRequestUrl.com/';$fp=fopen($url,'r');//file_get_contents($url);$headerArray = $http_response_header;$contentType = null;foreach($headerArray as $value){ if(contains('Content-Type',$value)){ $contentType = explode(':',explode(';',$value)[0])[1]; }}if($contentType!=null && contains("image",$contentType)){ echo "This is a image resource";}?> 如何获得页面中邮箱地址 zendframework,出现了连接中断,提示说连接被重置 求助php过滤非法字符的语句 sql语句执行非常慢,高手请指点 php 的strtok()的函数 如何用php获得某个文件夹的大小啊? 有没有人知道??? 求SESSION的解决新方案!!! 怎么实现“打开对话框”? 新手请教大家一个问题啊,谢谢了 正则表达式的一个问题 mysql 数值类型问题请教
<?php
$filename='filename';
file_put_contents($filename,$str);
if($imginfo = @getimagesize($filename)){
//以下进行你要的处理
echo "{$filename}是一张图片";
}else{
//以下进行不是图片文件的处理
echo "{$filename}不是图片文件";
@unlink($filename);
}
在确保请求的图片资源存在的前提下,
可以通过判断获取http请求数据的contentType来判断其是否为图片,或者其它资源。当contentType包含image关键字的时候,就说明该资源是一个图片资源。<?php
function contains($str, $content, $ignorecase=true)
{
if ($ignorecase)
{
$str = strtolower($str);
$content = strtolower($content);
}
return strpos($content,$str) ? true : false;
}$url='http://yourRequestUrl.com/';$fp=fopen($url,'r');
//file_get_contents($url);$headerArray = $http_response_header;
$contentType = null;foreach($headerArray as $value){
if(contains('Content-Type',$value)){
$contentType = explode(':',explode(';',$value)[0])[1];
}
}if($contentType!=null && contains("image",$contentType)){
echo "This is a image resource";
}
?>