473,569 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if uploaded file is binary or ascii?

I have a script where a user can upload their csv file into a mysql
database. The problem is sometimes a user will upload the raw excel or
access file. How can I perform some simple checks to determine if the
file is binary. I could do some regular expressions to check for
unusual characters, but how would I read the file line by line?

Aug 31 '06 #1
5 10702

ve*****@yahoo.c om wrote:
I have a script where a user can upload their csv file into a mysql
database. The problem is sometimes a user will upload the raw excel or
access file. How can I perform some simple checks to determine if the
file is binary. I could do some regular expressions to check for
unusual characters, but how would I read the file line by line?
Just look for \x00. Binary files would inevitable have it.

Aug 31 '06 #2

Chung Leong wrote:
Just look for \x00. Binary files would inevitable have it.
Thanks. Is there a way to do it without reading in the entire file to
memory?

Aug 31 '06 #3
ve*****@yahoo.c om wrote:
Chung Leong wrote:
>>Just look for \x00. Binary files would inevitable have it.

Thanks. Is there a way to do it without reading in the entire file to
memory?
On linux/unix you can call "file" on the uploaded file.
Aug 31 '06 #4
<ve*****@yahoo. comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
>I have a script where a user can upload their csv file into a mysql
database. The problem is sometimes a user will upload the raw excel or
access file. How can I perform some simple checks to determine if the
file is binary. I could do some regular expressions to check for
unusual characters, but how would I read the file line by line?
how about check the type-value? It should tell the mime-type of the file.
Couldn't you use that to filter out the idiots?
$_FILES['userfile']['type']

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi. net || Gedoon-S @ IRCnet || rot13(xv***@bhg byrzcv.arg)
Aug 31 '06 #5
Marcin Dobrucki wrote:
ve*****@yahoo.c om wrote:
>Chung Leong wrote:
>>>Just look for \x00. Binary files would inevitable have it.

Thanks. Is there a way to do it without reading in the entire file to
memory?

On linux/unix you can call "file" on the uploaded file.
'file' is very good at guessing file types - but it's still a guess and
possible to circumvent if you're up to mischief.

A better solution is to 'cat -v' the file. Some versions of tr support
regexes too.

C.
Sep 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
15904
by: smilesinblues | last post by:
Hi, I have to allow my visitors to upload image on my site. I am using the follwoing code to do that: $uploaddir = 'admin/'; $uploadfile = $uploaddir . basename($_FILES); move_uploaded_file($_FILES, $uploadfile); My problem is that I expect them to use same file name, like all of
2
4401
by: Don | last post by:
Hi all, I need to read an uploaded file into chunks, so I can examine the acculative size between chunks to determine if a maximum size is being exceeded. I'm currently using the PHP function "move_uploaded_file()" to move it in one piece. But, I'm finding some clients files are bigger than I want to allow, and with that function I don't...
2
1828
by: ketulp_baroda | last post by:
Hi I want to determine the content type of uploaded file. File was uploaded by using <input type=file>. How can I determine the content type?
3
6177
by: Roberto | last post by:
I have the following problem: I have the following form client side: <FORM.......> <FORM action="./WZUpload.asp" method="Post" enctype="multipart/form-data" WIDTH=100%> <INPUT Type="file" name="UpFileName" STYLE="{Width:400px}"><BR>
6
10030
by: SandyMan | last post by:
Hi, I am able to open a binary file for reading but can someone tell me as how to go about converting a Binary file to ASCII file using C. Thanks In Advance SandyMan
68
5180
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
2
4929
by: h3m4n | last post by:
i have a short script that allows users to upload files, but when i try to check for a valid filesize (using fileitem) i get '-1' i can't find information about using filesize anywhere. any ideas? code: form = cgi.FieldStorage() fileitem = form print str(fileitem.filesize)
8
2819
by: ctiggerf | last post by:
I was hopeing someone could help me out here. Been stumped on this one all day. This function 1. Checks uploaded files. 2. Creates two resized images from each (a full size, and a thumbnail). 3. Returns an array with a bool (false if the upload failed), and an error message.
10
3829
by: maheswaran | last post by:
Hi, I want to check the extension of uploaded file. I upload the bmp image using file property. Now i am going to edit the file upload text as bmp file into gif... example : i upload c:/sample.bmp now i manually edit the file input box as c:/sample.gif and i upload this one. Now i want to check the orignial file ext.. is there...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.