• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make sure a file is a valid text file.  RSS feed

 
shivkanth majji
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a requirement where I have to read a file and store it in database . Before I read i have to make sure the file is a valid txt file. I tried reading the content type using some thing as below


This works if the user is trying to uplaod a valid .txt extension file. But, In some case user tries to upload a text file with out .txt extension. In those case the above code fails as the content type becomes application/octet-stream. Is there a better way to find out the content type of a file to make sure its a text file. Thanks in advance for the help.

Regards,

Shiv

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your definition of "a valid txt file"?
 
shivkanth majji
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A valid text file is any file that contains simple english text in it. I know it sounds a little dumb but, let me tell you a bit of the requirement. The code has to process license files associated with a product and these license files always have plain english text explaining the license terms. It is the format that causes the problem e.g. If i have a product Test the license file inside that product is named as Test.Test for some reason i dont know and have no control over. For these kind of files the content type comes as application/octet-strea, even though it is a simple text file. Is there a way around this problem.

I hope I conveyed the point....
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It'd be rather easy to make sure that all characters in a file are plain ASCII, no?
 
shivkanth majji
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you mean I basically have to parse the entire file and check for non ascii characters and if i find any then determine that the file is not a text file.
 
shivkanth majji
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to do something similar to file command in unix to determine the file type.
 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
As Ulf had suggested , it will be better to read the file and check it.
If the file has to contain simple english text then we still have to read the contents of the file right ?

And moreover how big can that license file be ?
Btw there is no encryption/decryption for your license file ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!