简体   繁体   中英

getimagesize() function not working with linux

I have this function

public static function upload($path_folder, $input_file_name) {

    $photo_file = $_FILES[$input_file_name]; // FILE NAME
    $photo_name = $photo_file['name']; // PHOTO NAME
    $photo_tmp_name = $photo_file['tmp_name'];  // TMP NAME
    $photo_extension = pathinfo($photo_name, PATHINFO_EXTENSION); // EXTENSION
    // soon GIFs

    $rand_num = random_int(1, 3);
    switch ($rand_num) {
        case 1:
            $hash = mb_strtoupper(Hash::create(), 'UTF-8');
        break;
        case 2:
            $hash = mb_strtolower(Hash::create(), 'UTF-8');
        break;
        case 3:
            $hash = str_shuffle(Hash::create());
        break;
        default:
            $hash = Hash::create();
        break;
    }

    $photo_with_extension = $hash . '.' . $photo_extension; // NEW FILE NAME
    $factory_destination = 'photos/factory/' . $photo_with_extension; // FACTORY

    if(move_uploaded_file($photo_tmp_name, $factory_destination)) {
    if(file_exists($factory_destination)) {
    if(
        ($photo_extension === 'jpg') ||
        ($photo_extension === 'jpeg') ||
        ($photo_extension === 'JPG') ||
        ($photo_extension === 'JPEG') ||
        ($photo_extension === 'png') ||
        ($photo_extension === 'PNG')
    ) {

        if(
            ($photo_extension === 'jpg') ||
            ($photo_extension === 'jpeg') ||
            ($photo_extension === 'JPG') ||
            ($photo_extension === 'JPEG')
        ) {
            if(imagecreatefromjpeg($factory_destination)) { // JPEG
                $photo_create = imagecreatefromjpeg($factory_destination);
            }
        }

        if(
            ($photo_extension === 'png') ||
            ($photo_extension === 'PNG')
        ) {
            if(imagecreatefrompng($factory_destination)) { // PNG
                $photo_create = imagecreatefrompng($factory_destination);
            }
        }

        if(getimagesize($photo_tmp_name)) {

            list($photo_width, $photo_height) = getimagesize($photo_tmp_name); // WIDTH & HEIGHT
            $photo_new_width = 300; // NEW WIDTH
            $photo_new_height = 300;
            //$photo_new_height = ($photo_height / $photo_width) * $photo_new_width; // NEW HEIGHT
            $true_color = imagecreatetruecolor($photo_new_width, $photo_new_height); // TRUE COLOR

            if(imagecopyresampled($true_color, $photo_create, 0, 0, 0, 0, $photo_new_width, $photo_new_height, $photo_width, $photo_height)) {


                $photo_copy = imagecopyresampled($true_color, $photo_create, 0, 0, 0, 0, $photo_new_width, $photo_new_height, $photo_width, $photo_height); // COPY
                    $mime = mime_content_type($factory_destination);

                    if(($mime === 'image/jpeg') || ($mime === 'image/png')) {

                        switch ($path_folder) {
                            case 1:
                                $folder = 'profile';
                            break;
                            case 2:
                                $folder = 'identities';
                            break;

                            default:
                                $folder = 'errors';
                            break;
                        }

                        $new_destination = 'photos/' . $folder . '/';
                        //$image = imagecreatefromstring(file_get_contents($photo_tmp_name));
                        $exif = exif_read_data($factory_destination);
                        if(!empty($exif['Orientation'])) {
                            switch($exif['Orientation']) {
                                case 8:
                                    $true_color = imagerotate($true_color,90,0);
                                    break;
                                case 3:
                                    $true_color = imagerotate($true_color,180,0);
                                    break;
                                case 6:
                                    $true_color = imagerotate($true_color,-90,0);
                                    break;
                            }
                        }
                        if(imagejpeg($true_color, $new_destination . $photo_with_extension, 75)) { // DESIGNATED PATHS
                            imagedestroy($true_color); // return 

                            if(unlink($factory_destination)) {
                                return $photo_with_extension; // AUTO UPDATE ADMIN THAT THERE IS A PROBLEM
                            } else { return 'unlink last'; }        
                        } else { return 'imagejpeg'; }
                    } else { return 'mime'; }
                /*move*/ 
            } else { return 'resampled'; }
        } else { return 'get image:' . $photo_tmp_name; }
    } else { return 'wrong ext'; }
    } else { return 'default2.png'; }
    } else { return 'move'; }
    return empty($photo_with_extension) ? '' : 'false';
}

the function works upto move_uploaded_file() after that nothing is happening.

This is on production, GD enabled and compatible, Linux server. Where do you think that my codes went wrong ? This works perfectly fine on my localhost though.

To be precise, the imagejpeg() is the one that is not working

New info;

It seems that the problem occurs on getimagesize($photo_tmp_name) it returns false

Newer info:

the tmp_name that given on getimagesize() is correct when I echo it, but it returns false.

如果在move_uploaded_file()函数之后,应该销毁tmp_file ,则可以将tmp_file替换为已移动的文件,因为php会阻止执行该过程,因此可以放心!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM