0
function upload(&$model){
    if($_SERVER['REQUEST_METHOD'] === 'POST'){
        $file = $_FILES['file'];
        $file_name = $file['name'];
        $file_ext2 = explode('.', $file_name);
        $file_extenstion = strtolower(end($file_ext2));
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        echo(finfo_file($finfo, $file['tmp_name']));
        echo($file['type']);
       }
    return "superliga_view";
}

Why

echo(finfo_file($finfo, $file['tmp_name'])); 

prints image/jpeg, but

echo($file['type']); 

prints "image/png"?

I try make watemark, but

$image = imagecreatefrompng($target);

sometimes throws errors due to wrong file type. With small files php always reads them as png, but with large files it reads them as jpeg.

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • 3
    Because `finfo` read from file header. It is more trustable. While `$_FILES['xx']['type']` read from browser/user input. It can be fake or wrong and untrustable. Use `finfo()` to detect real file's mime type. – vee Dec 28 '22 at 15:49
  • `$file_name`, `$file_ext2`, and `$file_extenstion` are not used. – user3783243 Dec 28 '22 at 15:50
  • How is it possible that when I upload a png, finfo reads it as a jpeg? – Hubert Wajda Dec 28 '22 at 16:02
  • You didn't say where you upload it to, so it's hard to know if the service you use changes the type - maybe to save space. You can look at the first 4 bytes of your file to see it contains a PNG https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header or JPEG signature. – Mark Setchell Dec 28 '22 at 16:33
  • 1
    If I have real .jpg file that was created from photo editor program, then I renamed it to image.png. Its real file's mime type is still be `image/jpeg` not `image/png`. This is one of example. – vee Dec 28 '22 at 16:41
  • Read more on these questions [1](https://stackoverflow.com/questions/59986082/php-how-to-properly-check-mime-type-of-a-file), [2](https://stackoverflow.com/questions/1232769/how-to-get-the-content-type-of-a-file-in-php/3664655#3664655). Maybe use these functions to check the result and compare with `finfo` [1](https://www.php.net/manual/en/function.mime-content-type.php), [2](https://www.php.net/manual/en/function.getimagesize.php). – vee Dec 28 '22 at 16:46
  • Here are all functions https://pastebin.com/s2EQehgQ. Errors appear on the 2. line. "Warning: imagecreatefrompng(): 'images/63ac870a54d3a1.14185028.png' is not a valid PNG file in /var/www/dev/src/controllers.php on line 30". I am sure that is a PNG file. – Hubert Wajda Dec 28 '22 at 18:19
  • 1
    Your code should be [mcve] and in your question itself and also not in the comments and definitely not a *picture* of your code... they are hard to run. Please click [edit] and update your question. – Mark Setchell Dec 28 '22 at 18:39

0 Answers0