Forum Moderators: coopster

Message Too Old, No Replies

PHP to CSV--and Back!

Adding to a CSV file

         

inuwolf

8:08 am on Jan 5, 2006 (gmt 0)

10+ Year Member



I have a sortable table in PHP that draws its data from a CSV file. This much works. But I have very limited PHP experience and I'm having trouble with this next step:

I'd like to create a simple form in PHP allowing a viewer to add a record (a row) in the CSV file. I've been unable to find a script as simple as I'd like--and trust me I have looked. If anyone has suggestions I greatly appreciate it.

While I am here, are there any precautions you recommend I take in making this table freely editable? I have a login system so I could require users to register before submitting data, but honestly I don't want to hamper anyone from contributing, at least initially. I could use a word/B.S. filter that prohibit invalid entries, if anyone has a script for that or any other anti-spamming measure.

Thanks, I appreciate all the help I've gotten since I registered at webmasterworld.

coopster

2:21 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



PHP's Filsystem functions [php.net] include a function called fgetcsv() [php.net] that sounds like something you would be interested in. There is an example in the manual pages showing how to use it as well.

Then, you could use the functions relative, fputcsv() [php.net] to write things back out.
fputcsv() is a PHP5 function, you may need to use some of the other functions to put the data back together and write it out to your file again if you are running a previous version of PHP.

As far as security, by all means you should be editing the data and making sure it is exactly what you expect it to be in terms of expression. So, the first column was supposed to be 'name', be certain it only contains characters or punctuation that you might normally find in a name.

inuwolf

5:45 pm on Jan 6, 2006 (gmt 0)

10+ Year Member



Thanks, I found a simple script that suits me, but I get this error:

Warning: fopen(datafile.csv): failed to open stream: HTTP wrapper does not support writeable connections. in /var/www/html/data.php on line 43
Cannot open data file

How do I make writeable connections? Is there any alternative to this; I feel it might be unsafe. Coopster, unfortunately I don't have enough experience to understand what you posted; I checked it out but it doesn't make sense to me.

jatar_k

5:51 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you trying to open the file using a web address(with http)?

you need to open it via the file system instead. Just put the local path to the file

inuwolf

8:55 pm on Jan 6, 2006 (gmt 0)

10+ Year Member



a stupid mistake by a stupid user. thanks