title: PHP PNG images are being uploaded with mime "image/jpeg" on KCFINDER 3.12
body: <p>At this point, I'm very confused for something an image called <strong>this-thing.png</strong> (created as PNG24 out of <em>photoshop CS5</em>), gets uploaded to <a href="https://kcfinder.sunhater.com/" rel="nofollow noreferrer"><code>kcfinder</code></a> as <code>image/jpeg</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/VA1RL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VA1RL.png" alt="enter image description here"></a></p>
  
  <p>Analyzed with <a href="https://web.archive.org/web/20190720212920/http://tridd.com/" rel="nofollow noreferrer">TriID</a>, shows the image is completely 100% PNG</p>
  
  <pre><code>C:\TrID&gt;trid C:\users\michael\downloads\this-thing.png
  
  TrID/32 - File Identifier v2.20 - (C) 2003-15 By M.Pontello
  Definitions found:  3790
  Analyzing...
  
  Collecting data from file: C:\users\michael\downloads\this-thing.png
  100.0% (.PNG) Portable Network Graphics (16000/1)
  </code></pre>
  
  <p>But when the variable data within <em>$_FILES[(key($_FILES)]['tmp_name']</em>, reaches <strong>kcfinder\core\class\browser.php</strong> function <strong>moveUploadFile</strong> (as the argument <strong>$file</strong>) ... the mime type from the tmp_name comes up as JPEG and not as PNG.</p>
  
  <p>Modified function, for testing purposes, included code from <a href="https://stackoverflow.com/questions/10464948/getimagesize-not-returning-false-when-it-should">getimagesize() not returning false</a></p>
  
  <pre><code>protected function moveUploadFile($file, $dir) {
  
      $message = $this-&gt;checkUploadedFile($file);
  
      if ($message !== true) {
          if (isset($file['tmp_name']))
              @unlink($file['tmp_name']);
          return "{$file['name']}: $message";
      }
  
      $filename = $this-&gt;normalizeFilename($file['name']);
      $target = "$dir/" . file::getInexistantFilename($filename, $dir);
  
      echo "&lt;h3&gt;PHP_FILES foreach&lt;/h3&gt;";
  
      foreach ($_FILES['upload']['name'] as $key =&gt; $value){    
          echo "&lt;pre&gt;";
          print_r(getimagesize($_FILES['upload']['tmp_name'][$key]));
          echo "&lt;/pre&gt;";
      }
  
      echo "&lt;h3&gt;TEMP FILE&lt;/h3&gt;";
      echo "&lt;/strong&gt;file variable&lt;/strong&gt;";
      echo "&lt;pre&gt;";
      print_r($file);
      echo "&lt;/pre&gt;";
  
      echo "&lt;/strong&gt;file.tmp_name&lt;/strong&gt;";
      echo "&lt;pre&gt;";
      print_r($file['tmp_name']);
      echo "&lt;/pre&gt;";
  
      $tmp_name_imagesize = getimagesize($file['tmp_name']);
  
      echo "&lt;pre&gt;";
      print_r($tmp_name_imagesize);
      echo "&lt;/pre&gt;";
  
      if (imagetypes() &amp; IMG_PNG) { echo "PNG Supported"; } else { echo "PNG not supported."; }
  
      // mkaatman - move tmp file to /tmp/ to check its MD5SUM result
      $temporary_file_path = $file['tmp_name'] . ".uploaded";
      move_uploaded_file($file['tmp_name'], $temporary_file_path);
  
      die();
  
  }
  </code></pre>
  
  <p>Result, during the upload of the file <strong>this-thing.png</strong>, shows that the media type is in fact <strong>JPG</strong> and not <strong>PNG</strong> (this is the part, I can't seem to wrap my head around).</p>
  
  <p><a href="https://i.stack.imgur.com/Nrpqr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nrpqr.png" alt="enter image description here"></a><br>
  <sub>(source: <a href="http://iforce.co.nz/i/kgcgf2af.pyo.png" rel="nofollow noreferrer">iforce.co.nz</a>)</sub>  </p>
  
  <p>Apparently this file <code>php92C2.tmp.uploaded</code> is the uploaded file, to the <code>/tmp/</code> directory.</p>
  
  <p><a href="https://i.stack.imgur.com/CqoSq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CqoSq.jpg" alt="enter image description here"></a></p>
  
  <p>I have added <code>.png</code> to the end of the file, using <code>Windows Rename</code>, for the purposes of file analysis.</p>
  
  <pre><code>C:\TrID&gt;trid C:\users\michael\downloads\php92C2.tmp.uploaded.png
  
  TrID/32 - File Identifier v2.20 - (C) 2003-15 By M.Pontello
  Definitions found:  3790
  Analyzing...
  
  Collecting data from file: C:\users\michael\downloads\php92C2.tmp.uploaded.png
   50.0% (.JPG) JFIF JPEG Bitmap (4003/3)
   37.4% (.JPG) JPEG Bitmap (3000/1)
   12.4% (.MP3) MP3 audio (1000/1)
  </code></pre>
  
  <p>But if the image is tested directly through PHP (copy+paste into a directory)</p>
  
  <pre><code>&lt;?php
  
  $image_file = "this-thing.png";
  $image_file_details = getimagesize($image_file);
  
  echo "&lt;pre&gt;";
  print_r($image_file_details);
  echo "&lt;/pre&gt;";
  
  ?&gt;
  </code></pre>
  
  <p>The result, reads that the image is in-fact PNG.</p>
  
  <pre><code>Array
  (
      [0] =&gt; 800
      [1] =&gt; 300
      [2] =&gt; 3
      [3] =&gt; width="800" height="300"
      [bits] =&gt; 8
      [mime] =&gt; image/png
  )
  </code></pre>
  
  <p>The form used within <strong>kcfinder/cache/base.js</strong> function <strong>function _.initUploadButton = function()</strong> is your basic, upload form.</p>
  
  <pre><code>&lt;div id="upload" style="top:5px;width:77px;height:31px"&gt;
      &lt;form enctype="multipart/form-data" method="post" target="uploadResponse" action="browse.php?type=image&amp;lng=en&amp;opener=ckeditor&amp;act=upload"&gt;&lt;input type="file" name="upload[]" onchange="_.uploadFile(this.form)" style="height:31px" multiple="multiple" /&gt;&lt;input type="hidden" name="dir" value="" /&gt;&lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Finally some information from KCFINDER.Config from <code>php</code></p>
  
  <pre><code>CONFIG.imageDriversPriority
  
  imagick gmagick gd
  
  CONFIG.deniedExts
  
  exe com msi bat php phps phtml php3 php4 cgi pl htaccess htm html
  
  CONFIG.types
  
  Array
  (
      [image] =&gt; 7z aiff asf avi bmp csv doc fla flv gif gz gzip jpeg jpg mid mov mp3 mp4 mpc mpeg mpg ods odt pdf png ppt pxd qt ram rar rm rmi rmvb rtf sdc sitd swf sxc sxw tar tgz tif tiff txt vsd wav wma wmv xls xml zip
      [images] =&gt; 7z aiff asf avi bmp csv doc fla flv gif gz gzip jpeg jpg mid mov mp3 mp4 mpc mpeg mpg ods odt pdf png ppt pxd qt ram rar rm rmi rmvb rtf sdc sitd swf sxc sxw tar tgz tif tiff txt vsd wav wma wmv xls xml zip
      [files] =&gt; 7z aiff asf avi bmp csv doc fla flv gif gz gzip jpeg jpg mid mov mp3 mp4 mpc mpeg mpg ods odt pdf png ppt pxd qt ram rar rm rmi rmvb rtf sdc sitd swf sxc sxw tar tgz tif tiff txt vsd wav wma wmv xls xml zip
      [uploads] =&gt; 7z aiff asf avi bmp csv doc fla flv gif gz gzip jpeg jpg mid mov mp3 mp4 mpc mpeg mpg ods odt pdf png ppt pxd qt ram rar rm rmi rmvb rtf sdc sitd swf sxc sxw tar tgz tif tiff txt vsd wav wma wmv xls xml zip
      [mimages] =&gt; *mime image/gif image/png image/jpeg
  )
  </code></pre>
  
  <p>Based on all of this information, I can't seem to figure out why on earth an image being uploaded as PNG, comes back as JPEG.</p>
  
  <p><strong>EDIT: I have tested, kcfinder using image created from mspaint (this is where it gets confusing)</strong></p>
  
  <p>The tested PNG image.</p>
  
  <p><a href="https://i.stack.imgur.com/bYWok.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bYWok.png" alt="enter image description here"></a></p>
  
  <p>The result (based on the above code).</p>
  
  <p><a href="https://i.stack.imgur.com/YVldT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YVldT.png" alt="enter image description here"></a></p>
  
  <pre><code>C:\TrID&gt;trid C:\users\michael\Pictures\breaking-bad.png
  
  TrID/32 - File Identifier v2.20 - (C) 2003-15 By M.Pontello
  Definitions found:  3790
  Analyzing...
  
  Collecting data from file: C:\users\michael\Pictures\breaking-bad.png
  100.0% (.PNG) Portable Network Graphics (16000/1)
  
  C:\TrID&gt;
  </code></pre>
  
  <p><strong>EDIT: PNG Support (reply for markman)</strong></p>
  
  <pre><code>if (imagetypes() &amp; IMG_PNG) {
      echo "PNG Supported";
  } else {
      echo "PNG not supported.";
  }
  </code></pre>
  
  <p><strong>EDIT: I've found where the image is being converted from PNG to JPG</strong></p>
  
  <p>if <code>checkUploadedFile</code> is commented out during <code>moveUploadFile</code> the file <code>this-thing.png</code> comes out as expected PNG....</p>
  
  <pre><code>protected function checkUploadedFile(array $aFile=null) {
      $config = &amp;$this-&gt;config;
      $file = ($aFile === null) ? $this-&gt;file : $aFile;
  
      if (!is_array($file) || !isset($file['name']))
          return $this-&gt;label("Unknown error");
  
      if (is_array($file['name'])) {
          foreach ($file['name'] as $i =&gt; $name) {
              $return = $this-&gt;checkUploadedFile(array(
                  'name' =&gt; $name,
                  'tmp_name' =&gt; $file['tmp_name'][$i],
                  'error' =&gt; $file['error'][$i]
              ));
              if ($return !== true)
                  return "$name: $return";
          }
          return true;
      }
  
      $extension = file::getExtension($file['name']);
      $typePatt = strtolower(text::clearWhitespaces($this-&gt;types[$this-&gt;type]));
  
      // CHECK FOR UPLOAD ERRORS
      if ($file['error'])
          return
              ($file['error'] == UPLOAD_ERR_INI_SIZE) ?
                  $this-&gt;label("The uploaded file exceeds {size} bytes.",
                      array('size' =&gt; ini_get('upload_max_filesize'))) : (
              ($file['error'] == UPLOAD_ERR_FORM_SIZE) ?
                  $this-&gt;label("The uploaded file exceeds {size} bytes.",
                      array('size' =&gt; $_GET['MAX_FILE_SIZE'])) : (
              ($file['error'] == UPLOAD_ERR_PARTIAL) ?
                  $this-&gt;label("The uploaded file was only partially uploaded.") : (
              ($file['error'] == UPLOAD_ERR_NO_FILE) ?
                  $this-&gt;label("No file was uploaded.") : (
              ($file['error'] == UPLOAD_ERR_NO_TMP_DIR) ?
                  $this-&gt;label("Missing a temporary folder.") : (
              ($file['error'] == UPLOAD_ERR_CANT_WRITE) ?
                  $this-&gt;label("Failed to write file.") :
                  $this-&gt;label("Unknown error.")
          )))));
  
      // HIDDEN FILENAMES CHECK
      elseif (substr($file['name'], 0, 1) == ".")
          return $this-&gt;label("File name shouldn't begins with '.'");
  
      // EXTENSION CHECK
      elseif (
          (substr($file['name'], -1) == ".") ||
          !$this-&gt;validateExtension($extension, $this-&gt;type)
      )
          return $this-&gt;label("Denied file extension.");
  
      // SPECIAL DIRECTORY TYPES CHECK (e.g. *img)
      elseif (preg_match('/^\*([^ ]+)(.*)?$/s', $typePatt, $patt)) {
          list($typePatt, $type, $params) = $patt;
          $class = __NAMESPACE__ . "\\type_$type";
          if (class_exists($class)) {
              $type = new $class();
              $cfg = $config;
              $cfg['filename'] = $file['name'];
              if (strlen($params))
                  $cfg['params'] = trim($params);
              $response = $type-&gt;checkFile($file['tmp_name'], $cfg);
              if ($response !== true)
                  return $this-&gt;label($response);
          } else
              return $this-&gt;label("Non-existing directory type.");
      }
  
      // IMAGE RESIZE
      $img = image::factory($this-&gt;imageDriver, $file['tmp_name']);
      if (!$img-&gt;initError &amp;&amp; !$this-&gt;imageResize($img, $file['tmp_name']))
          return $this-&gt;label("The image is too big and/or cannot be resized.");
  
      return true;
  }
  </code></pre>
  
  <p>Output is <code>PNG Supported.</code></p>