cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the original file extension

Former Member
0 Kudos

Hi Experts,

I have a requirement like the user can upload only .doc and .pdf files. In case the user renames say an .exe file to any of the acceptable files like .doc -even though he has renamed the file to a acceptable file extension the upload should restrict it.

I already converted the file content to xstring.

How can i get the original file type from the xstring or how can i come to know the original file type.

Kindly help

Best Regards,

Vinod

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Vinod,

There's no ultimate solution to this, but you could apply some heuristics to guess the file type. This is what the Unix file program does and it basically uses the so-called [magic numbers|http://en.wikipedia.org/wiki/File_format#Magic_number] (there's also Windows ports out there). For most files this means simply looking at the first few bytes. E.g. a PDF file should always start with a %PDF (first 4 bytes of the file). Similarly for Microsoft Word documents you can also find the bytes, best start is [Gary Kessler's file signature table|http://www.garykessler.net/library/file_sigs.html].

Keep in mind though that this is just an indicator. I.e. of course I could have a text file which starts with %PDF and it's still not a PDF document. However, such logic should still be good enough to filter out Windows executables (<em>.exe</em>) and others.

Anyhow, just check the first couple of bytes of your XSTRING for the magic numbers that you want to allow and reject anything else...

Cheers, harald

Former Member
0 Kudos

Hi Harald,

Thanks a lot for the suggestion. I have implemented the magic number concept and it works fine for my requirement :).

I have implemented the below code. ( Posting the code so some one who has the same requirement can refer to it. I have not tested for all the file types; if someone wants to implement the solution kindly check)

DATA: lv_xstring TYPE xstring,

lv_valid TYPE xfeld,

lv_temp_xstring TYPE string.

CASE lv_filetype.

WHEN 'PDF'.

IF lv_xstring+0(4) EQ '25504446'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'BMP'.

IF lv_xstring+0(2) EQ '424d' OR

lv_xstring+0(2) EQ '424D'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'DOC'.

IF lv_xstring0(4) EQ 'ECA5C100' OR lv_xstring0(4) EQ 'D0CF11E0'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'DOCX'.

MOVE lv_xstring+0(9) TO lv_temp_xstring.

IF lv_temp_xstring+0(17) EQ '504B0304140006000'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'GIF'.

IF lv_xstring+0(4) EQ '47494638'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'JPEG'.

IF lv_xstring+0(4) EQ 'FFD8FFE0'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'JPG'.

IF lv_xstring+0(4) EQ 'FFD8FFE0'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'PPT'.

IF lv_xstring+0(4) EQ '006E1EF0' OR

lv_xstring+0(4) EQ '0F00E803' OR

lv_xstring+0(4) EQ 'A0461DF0' OR

lv_xstring+0(4) EQ 'D0CF11E0'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'PPTX'.

MOVE lv_xstring+0(9) TO lv_temp_xstring.

IF lv_temp_xstring+0(17) EQ '504B0304140006000'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'TIF'.

IF lv_xstring+0(4) EQ '49492A00'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'TIFF'.

IF lv_xstring+0(4) EQ '49492A00'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'XLS'.

MOVE lv_xstring+0(4) TO lv_temp_xstring.

IF lv_xstring+0(7) EQ '09081000000605' OR

lv_temp_xstring+0(8) EQ 'FDFFFFFF' OR

lv_temp_xstring+0(8) EQ 'D0CF11E0'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

WHEN 'XLSX'.

MOVE lv_xstring+0(9) TO lv_temp_xstring.

IF lv_temp_xstring+0(17) EQ '504B0304140006000'.

lv_valid = 'X'.

ELSE.

CLEAR lv_valid.

ENDIF.

ENDCASE.

Cheers,

Vinod

Answers (0)