【问题标题】:File Upload Deny Animated GIFs (using PHP/Zend Framework)文件上传拒绝动画 GIF(使用 PHP/Zend 框架)
【发布时间】:2010-07-29 11:01:11
【问题描述】:

我想我可能希望用户能够上传静态 GIF,但不能上传动画。说化身,因为他们可能看起来......不专业和分散注意力。 PHP 或 Zend 框架中是否有一种方法可以验证文件上传?

【问题讨论】:

    标签: php validation zend-framework file-upload


    【解决方案1】:

    您可以使用gd 库来保存您的图像。对于 gif 类型的文件,如果动画,则仅保存 gif 文件中的第一帧。有关如何使用它的更多信息,请参阅imagegif 函数。

    【讨论】:

      【解决方案2】:

      形成PHP: imagecreatefromgif - Manual

      I wrote two alternate versions of ZeBadger's is_ani() function, for determining if a gif file is animated
      
      Original:
      http://us.php.net/manual/en/function.imagecreatefromgif.php#59787
      
      The first alternative version is just as memory intensive as the original, and more CPU intensive, but far simpler:
      
      <?php
      function is_ani($filename) {
          return (bool)preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', file_get_contents($filename));
      }
      ?>
      
      The second alternative is about as CPU intensive as the original function, but uses less memory (and may also result in less disk activity)
      
      <?php
      function is_ani($filename) {
          if(!($fh = @fopen($filename, 'rb')))
              return false;
          $count = 0;
          //an animated gif contains multiple "frames", with each frame having a
          //header made up of:
          // * a static 4-byte sequence (\x00\x21\xF9\x04)
          // * 4 variable bytes
          // * a static 2-byte sequence (\x00\x2C)
      
          // We read through the file til we reach the end of the file, or we've found
          // at least 2 frame headers
          while(!feof($fh) && $count < 2)
              $chunk = fread($fh, 1024 * 100); //read 100kb at a time
              $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00\x2C#s', $chunk, $matches);
      
          fclose($fh);
          return $count > 1;
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-30
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        • 2013-04-22
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多