0

该代码检查上传是否正常,以及已上传的文件格式是否正确,但我总是收到“您上传的第一张图片不属于受支持的文件类型”的响应。从剧本。我不知道为什么会这样。

if ( isset( $_POST[ 'submit' ] ) ) {
                    $dir_1   = './images/';
                    $thumb_1 = '.images/thumb';
                    //making sure the uploaded file transfer was successful
                    if ( $_FILES[ 'pic1' ][ 'error' ] != UPLOAD_ERR_OK ) {
                            switch ( $_FILES[ 'pic1' ][ 'error' ] ) {
                                    case UPLOAD_ERR_INI_SIZE:
                                            die( 'The uploaded 1st image exceeds the upload_max_filesize directive ' . 'in php.ini.' );
                                            break;
                                    case UPLOAD_ERR_FORM_SIZE:
                                            die( 'The uploaded 1st exceeds the MAX_FILE_SIZE directive that ' . 'was specified in the HTML form.' );
                                            break;
                                    case UPLOAD_ERR_PARTIAL:
                                            die( 'The uploaded 1st image was only partially uploaded.' );
                                            break;
                                    case UPLOAD_ERR_NO_FILE:
                                            die( 'No 1st image was uploaded.' );
                                            break;
                                    case UPLOAD_ERR_NO_TMP_DIR:
                                            die( 'The server is missing a temporary folder.' );
                                            break;
                                    case UPLOAD_ERR_CANT_WRITE:
                                            die( 'The server failed to write the uploaded the uploaded 1st image to disk.' );
                                            break;
                                    case UPLOAD_ERR_EXTENSION:
                                            die( '1st image upload stopped by extension.' );
                                            break;
                            } //$_FILES[ 'pic1' ][ 'error' ]
                    } //$_FILES[ 'pic1' ][ 'error' ] != UPLOAD_ERR_OK
                    // making sure the file is being uploaded

                    $error = 'The 1st image you have uploaded was not of supported filetype.';
                    switch ( $type ) {
                            case IMAGETYPE_GIF:
                                    $image_1 = imagecreatefromgif( $_FILES[ 'pic1' ][ 'tmp_name' ] ) or die( $error );
                                    break;
                            case IMAGETYPE_JPEG:
                                    $image_1 = imagecreatefromjpeg( $_FILES[ 'pic1' ][ 'tmp_name' ] ) or die( $error );
                                    break;
                            case IMAGETYPE_PNG:
                                    $image_1 = imagecreatefrompng( $_FILES[ 'pic1' ][ 'tmp_name' ] ) or die( $error );
                                    break;
                            default:
                                    die( $error );                                        
                    } //$type
                      $image_date_1 = @date( 'Y-m-d' );
                     list( $width, $height, $type, $attr ) = getimagesize( $_FILES[ 'pic1' ][ 'tmp_name' ] );
}
4

1 回答 1

1

您正在将 $type 用于 switch 语句。但是在切换后调用 getimagesize 函数的这一行中获取 $type 值。

list( $width, $height, $type, $attr ) = getimagesize( $_FILES[ 'pic1' ][ 'tmp_name' ] );

如果在 switch 语句之前将此行向上移动。它应该可以解决问题。

if ( isset( $_POST[ 'submit' ] ) ) {
                $dir_1   = './images/';
                $thumb_1 = '.images/thumb';
                //making sure the uploaded file transfer was successful
                if ( $_FILES[ 'pic1' ][ 'error' ] != UPLOAD_ERR_OK ) {

                        switch ( $_FILES[ 'pic1' ][ 'error' ] ) {
                                case UPLOAD_ERR_INI_SIZE:
                                        die( 'The uploaded 1st image exceeds the upload_max_filesize directive ' . 'in php.ini.' );
                                        break;
                                case UPLOAD_ERR_FORM_SIZE:
                                        die( 'The uploaded 1st exceeds the MAX_FILE_SIZE directive that ' . 'was specified in the HTML form.' );
                                        break;
                                case UPLOAD_ERR_PARTIAL:
                                        die( 'The uploaded 1st image was only partially uploaded.' );
                                        break;
                                case UPLOAD_ERR_NO_FILE:
                                        die( 'No 1st image was uploaded.' );
                                        break;
                                case UPLOAD_ERR_NO_TMP_DIR:
                                        die( 'The server is missing a temporary folder.' );
                                        break;
                                case UPLOAD_ERR_CANT_WRITE:
                                        die( 'The server failed to write the uploaded the uploaded 1st image to disk.' );
                                        break;
                                case UPLOAD_ERR_EXTENSION:
                                        die( '1st image upload stopped by extension.' );
                                        break;
                        } //$_FILES[ 'pic1' ][ 'error' ]
                } //$_FILES[ 'pic1' ][ 'error' ] != UPLOAD_ERR_OK
                // making sure the file is being uploaded

                $error = 'The 1st image you have uploaded was not of supported filetype.';
                  list( $width, $height, $type, $attr ) = getimagesize( $_FILES[ 'pic1' ][ 'tmp_name' ] );
                switch ( $type ) {
                        case IMAGETYPE_GIF:
                                $image_1 = imagecreatefromgif( $_FILES[ 'pic1' ][ 'tmp_name' ] ) or die( $error );
                                break;
                        case IMAGETYPE_JPEG:
                                $image_1 = imagecreatefromjpeg( $_FILES[ 'pic1' ][ 'tmp_name' ] ) or die( $error );
                                break;
                        case IMAGETYPE_PNG:
                                $image_1 = imagecreatefrompng( $_FILES[ 'pic1' ][ 'tmp_name' ] ) or die( $error );
                                break;
                        default:
                                die( $error );                                        
                } //$type
                  $image_date_1 = @date( 'Y-m-d' );

}
于 2013-10-08T09:06:24.320 回答