无法使用exif_read_data提取$ _FILES的内容

时间:2013-02-17 00:51:01

标签: php arrays

我刚接触使用php,并且在过去的几天里一直都很难过。 我试图提取并使用从$ _FILES中的多个数组中提取的键值对。我确信这是一个荒谬的愚蠢问题,但我很难过。如果我运行以下代码,我会得到以下结果。

$exif = exif_read_data('/uploads/'.$file['name'],  0, true);

  foreach($_FILES as $file){
  echo $file['name']  .  " :<br />\n";
  foreach ($exif as $key => $section) {
      foreach ($section as $name => $val) {

          echo "<pre>This is \$name" . $name ."<br>";
          echo "<pre>This is \$val" . $val ."<br>";
          }
        }
     } 

以下是输出:

上传的图片

Image contains headers  
Across the Field - LK.jpg :
FILE.FileName: Across the Field - LK.jpg
FILE.FileDateTime: 1361055472  
FILE.FileSize: 294785  
FILE.FileType: 2  
FILE.MimeType: image/jpeg  
FILE.SectionsFound: ANY_TAG, IFD0, THUMBNAIL, EXIF  
COMPUTED.html: width="700" height="525"  
COMPUTED.Height: 525  
COMPUTED.Width: 700  
COMPUTED.IsColor: 1  
COMPUTED.ByteOrderMotorola: 0  
COMPUTED.ApertureFNumber: f/9.0  
COMPUTED.Copyright: Copyright 2010   
IFD0.ImageWidth: 1024  
IFD0.ImageLength: 768  
IFD0.BitsPerSample: Array  
IFD0.PhotometricInterpretation: 2  
IFD0.Model: NIKON D300  
IFD0.Orientation: 1  
IFD0.SamplesPerPixel: 3  
IFD0.XResolution: 720000/10000 
IFD0.YResolution: 720000/10000    
IFD0.ResolutionUnit: 2  
IFD0.Software: Adobe Photoshop CS5 Windows  
IFD0.DateTime: 2013:01:24 16:33:16  
IFD0.Artist: lken  
IFD0.Copyright: Copyright 2010   
IFD0.Exif_IFD_Pointer: 316  
THUMBNAIL.Compression: 6
THUMBNAIL.XResolution: 72/1  
THUMBNAIL.YResolution: 72/1 
THUMBNAIL.ResolutionUnit: 2  
THUMBNAIL.JPEGInterchangeFormat: 642  
THUMBNAIL.JPEGInterchangeFormatLength: 0  
EXIF.ExposureTime: 1/100  
EXIF.FNumber: 9/1  
EXIF.ISOSpeedRatings: 400  
EXIF.ExifVersion: 0221  
EXIF.DateTimeOriginal: 2010:08:15 05:57:17  
EXIF.DateTimeDigitized: 2010:08:15 05:57:17 
EXIF.ShutterSpeedValue: 6643856/1000000   
EXIF.ApertureValue: 633985/100000  
EXIF.FocalLength: 1700/100  
EXIF.ColorSpace: 65535  
EXIF.ExifImageWidth: 700   
EXIF.ExifImageLength: 525  

为了简洁起见,我遗漏了第二张照片中的数据。

以下是$ _FILES的输出:

Array
(
    [userfile] => Array
        (
            [name] => Across the Field - LK.jpg
            [type] => image/jpeg
            [tmp_name] => C:\wamp\tmp\php223.tmp
            [error] => 0
            [size] => 294785
        )

    [userfile1] => Array
        (
            [name] => autumn-panorama - lk.jpg
            [type] => image/jpeg
            [tmp_name] => C:\wamp\tmp\php224.tmp
            [error] => 0
            [size] => 106349
        )
)

我查看了每个网站和每本书,我都能找到如何访问名称值对(例如IFD0.ImageWidth:1024和IFD0.ImageLength:768),这样我就可以设置对上传大小等的限制。

我现在非常困惑,并会感谢任何帮助或指示。特别感谢资源。谢谢。

1 个答案:

答案 0 :(得分:1)

嗯,该代码片段已经完成了这项工作,因此对于您的第一个文件,您应该可以执行以下操作。

$exif = exif_read_data($_FILES['userfile']['tmp_name'],  0, true);

if (isset($exif['IFD0']['ImageWidth']) && isset($exif['IFD0']['ImageLength']))
{
    // good to go!
    $width = $exif['IFD0']['ImageWidth'];
    $height = $exif['IFD0']['ImageLength'];
}
else
{
    // size data not present, fallback onto something else
    return;
}

现在,说到这一点,你应该信任exif数据来进行约束验证! Exif数据是元数据。它们可以很容易地被欺骗/注入,或者根本不存在。而是通过首先检查文件大小本身,然后使用getimagesize()来分析实际图像数据。见http://www.php.net/manual/en/function.getimagesize.php