如何判断一个数据是不是图片 是这样的,我需要获取远程的图片,然后写入本地但是有时候会出错误,比如获取了一个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";}?> php 如何判断时间连续性? 招聘网的软件环境和开发工具怎么写 浏览 php文件 报错 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@怎么在网页里嵌入 doc , xls 等文件.. 谢谢 php如何处理附件!!! 哪位大哥有PHP的SESSION做的购物车的类,急 css为什么不起作用了? 按钮延迟生效的问题! 我下载了一个windows2000下的apache+mysql+php4三合一自动安装包,安装后不能正常运行,如何把他卸载 一个关于表单提交的问题 正则表达式的一个问题 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";
}
?>