tags:

views:

34

answers:

4

I'm looking to create an easy way for a user to create a table, and upload it to the server using ftp. On the server side, I'd like to query this table like an SQL-like query.

As I'd like the user to edit this in something like OO Calc, or MS Excel, would csv files be the best/fastest to parse? Is doing a fgetcsv a good way? Could you suggest any alternatives?

+1  A: 

Your best bet is to allow users to upload a CSV file with the first line containing field names (and maybe field types (i.e. int, varchar, etc)). Then you can parse it and validate it for valid/malicious data. If it passes inspection then create a database table escaping all relevant data from which you then can query from.

This way not only do you validate the data first, which is always a good idea, but you control everything like the naming of tables, etc, which helps to keep your data out of malicious hands.

John Conde
A: 

If the user needs to create table give him access to phpMyAdmin (of course properly secured). That can support uploading data, too.

Unreason
The user needs to be able to add columns easily, and add the data one column at a time.
Adam
A: 

I think CSV would be the easiest thing to work with and will be very fast. Check out the fgetcsv() function. You can read the file into an array and search through that. If your files aren't huge and your queries are standard, then you can write your own search code and not worry about using a database.

If you need to handle any query your user types in, then you'll need to move the data into an SQL compatible database to query.

Scott Saunders
Could this cause any slowdown, if I regularly parse, like 500kb csv file?
Adam
It will probably take a second or two to parse a file that large. If this code will be used many times a second, it's definitely not how you want to handle the problem. The way I've described is the Easy way to code it, not necessarily the best way. :) You'll have to balance your server load needs against your development time, number of hits against how often the file will change, etc.
Scott Saunders
A: 

I've done this before using XML. The nice thing about this is that you can insert data that isn't flat (parent child relationships). If someone is having you load a table it is likely that eventually they will ask you do include child information.

Abe Miessler