php使用curl从url保存图像,但首先验证它
我发现下面的函数搜索得很好php使用curl从url保存图像,但首先验证它,php,file-upload,curl,Php,File Upload,Curl,我发现下面的函数搜索得很好 function grab_image($url,$saveto){ $ch = curl_init ($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $raw=curl_exec($ch); curl_c
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
这应该可以用来保存图像,但是我如何首先检查图像的维度
、大小
和文件类型
,以验证其有效性?类似的内容
$raw = curl_exec($ch);
if ($raw === FALSE) {
die("Fetch failed");
}
// get size
$size = strlen($raw);
// get dimensions
$img = imagecreatefromstring($raw);
$x = imagesx($img);
$y = imagesy($img);
// get mime type
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->buffer($raw); // e.g. image/jpeg
在启动curl之前,可以使用直接读取图像
$url = 'http://exmaple.com/img/some.png';
$imgInfo = getimagesize($url);
print_r($imgInfo);
我不能像上传文件一样处理照片上传过程吗?或者我需要使用上述步骤创建一个新函数?-上面的strlen($row)不起作用,因为$row是一个资源。您可以使用
文件获取内容($\u FILES['file']['tmp\u name'])
将上传的文件拖入变量,然后使用基于字符串的验证。或者将卷曲提取的文件写入磁盘,然后使用基于磁盘的验证。但我没有上载图像。。我正在使用一个URL来获取图像并对其进行验证…但是如何获取其文件大小?有多大kb,mb?在curl设置中,将CURLOPT_头设置为true,这样头将具有内容长度: