1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XF 1.5 The contents of the uploaded image do not match the file's extension

Discussion in 'XenForo Questions and Support' started by egorych, Nov 6, 2015.

  1. egorych

    egorych Member

    Hello,

    The error "The contents of the uploaded image do not match the file's extension" happens when you upload .jpg file while the image format is .png or whatever. I know I can convert image by my hands to desired format and upload the fixed one... But for common forum visitors it's a very annoying and frustrating error.

    Is there a way to fix it and forget about the term 'wrong image type' entirely? For example, I have ImageMagic installed, well is there a way to order xenforo "if image is of wrong type -> convert it so it fits requirements"?

    Thanks for your support.
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    The file doesn't need to be converted it just needs the extension to be changed. There isn't a way to change the system to ignore that error.
     
  3. Mr Lucky

    Mr Lucky Well-Known Member

    I've never heard of the, why would a png file have the .jpg extension in the first place?
     
  4. egorych

    egorych Member

    Thank you for your response @Chris D !

    this check is performed in library/Xenforo/Upload.php
    Code:
            $type = $imageInfo['type'];
            $extensionMap = array(
                IMAGETYPE_GIF => array('gif'),
                IMAGETYPE_JPEG => array('jpg', 'jpeg', 'jpe'),
                IMAGETYPE_PNG => array('png')
            );
            if (!isset($extensionMap[$type]))
            {
                return; // only consider gif, jpeg, png to be images in this system
            }
            if (!in_array($this->_extension, $extensionMap[$type]))
            {
                 $this->_errors['extension'] = new XenForo_Phrase('contents_of_uploaded_image_do_not_match_files_extension');
                return;
            }
    
    well, in order to tell Xenforo rename files on-the-fly without disturbing user I think we can do something like this:

    Code:
            if (!in_array($this->_extension, $extensionMap[$type]))
            {
                $filename_noext = str_replace($this->_extension,'',$this->_fileName);
                $this->_extension = $extensionMap[$type][0]; // set the extention desired
                $this->_fileName = $filename_noext . $this->_extension;
                // $this->_errors['extension'] = new XenForo_Phrase('contents_of_uploaded_image_do_not_match_files_extension');
                //return;
            }
    
    is that correct? do I need to call
    Code:
    setFileName ($fileName);
    or something similar once again to tell Xenforo rename the file name to be created on disk? Sorry for possible syntax mistakes, I'm not a programmer.

    Thanks in advance!

    P. S. @Mr Lucky, users often save files on computer with the filename which already exists (when making screenshots etc.). In windows, for example, they click "save", then doubleclick the existent file to rewrite it, "rewrite this file?" -> "yes". Previous file could be saved as .jpg, the next one has .png format but ends up been saved with .jpg extension. It happens a lot.
     
    Last edited: Nov 6, 2015

Share This Page