Jump to content

load file and execute


Lonig

Recommended Posts

I'm sorry for these last couple of posts... I know they are basic. Learning too many languages at once and its destroying my productivity. Sadly, I'm rather behind on php now as I havn't used it in about 8 months.Question: How do I load a file(.csv) and do calculations on it without UPLOADing the file?I was referred to the fgetcsv and I am trying to get it done without a mysql database. Unfortunatly, all the coding examples below on the php manual are about loading into databases I believe. Plus, I seem to recall it was pretty easy to do what I want, and I'm just forgetting something simple.Advice is appreciated. Or point me at an example of how to load a file (POST or GET) and do wonders to it without upload it to a site or inserting into a database.

Link to comment
Share on other sites

If you're using POST then you are uploading the file, what are you trying to do? If you have the file on disk then you can use fgetcsv to access it line-by-line. You can also read the entire file into a string and then use str_getcsv to do something similar. You have to have the file on disk though, regardless of whether or not it was just uploaded.

Link to comment
Share on other sites

There are very functions that simplfy the task of grabbing data on another HTTP server. But as I recall your csv file is on your desktop, right? Yeah. You'll have to upload it. You might not have to save it. I'm thinking you can read the data straight out of the temp file created on upload (?????), process it, display the results, and the temp file will be deleted when the script is done executing. So that simplfies a few things.Are you unsure of upload procedures? There's a good tutorial here.

Link to comment
Share on other sites

I'm thinking you can read the data straight out of the temp file created on upload (?????),
Yes, the temp file remains for the entire script's execution. So you can just get $_FILES['userfile']['tmp_name'] and read from that. The temp file will be automatically discarded when the script ends.
Link to comment
Share on other sites

Hmm... I'm thinking this isn't what I'm looking for on the script. But I'm also not sure I want to use any of this for the methods...Heres what I am doing:1) Taking data from Excel (any format savable by Excel 03)2) Inserting it in a webpage(preferably upload field, but can copy/paste I guess)3) Having the webpage calculate the results.4) I do not care if the data is viewable after its calculates. I guess it won't hurt, but the data changes so fast, it isn't a huge benefit to store it in a MySQL form. Again, I can if its easier/better/less buggy.Problems:1) CSV files from Excel seem to have a wierd syntax compared to the MySql csv. (; and spaces)2) I'm not sure if its viable to do this without possible temp file buildup(in the case of failed uploads) or even sorting them properly via headers. Since again, Excel seems to have a different way of putting headers into a csv file.So... thats my dilema. This is just a hobby project I'm doing, just to clarify.The temp file thing that this thread has shown me is making me wonder if its just not smarter to upload the csv into a mysql database. Any links you all have on that (or a simple code snippet) would be great. the FGETCSV seems to be the way to go for that situation, but everyone posting examples in it are going overboard I think.Thanks in advance,Lonig

Link to comment
Share on other sites

4) I do not care if the data is viewable after its calculates. I guess it won't hurt, but the data changes so fast, it isn't a huge benefit to store it in a MySQL form. Again, I can if its easier/better/less buggy.
To clarify the above: I mean I don't care if its viewable by others, just the original person... ie: doesn't need to generate a permanent page or store in database...
Link to comment
Share on other sites

I'm really not clear on the project, which is okay. It sounds like you want some processing and you can't get that out of excel itself? Could it be that this is the real issue?You could just create a text area and paste csv data right into it, then have javascript process it and spit out the results.Or upload the data to a php script, through file upload OR paste the csv data right into a textarea. That'd be more time consuming manually, but the code would be (slightly) easier to write.Or heck, you could even process your data using a BASIC interpreter on your desktop, and then you wouldn't have to worry about your browser at all. I'm actually serious about this. You wouldn't have all of PHP's string manipulation functions, but they aren't that hard to write.So I'm wondering now: What exactly do you need? Why doesn't Excel do what you need? Is PHP really the ideal way to process your data? What is the result you're looking for?--another csv, something that can be printed, something that looks pretty?

Link to comment
Share on other sites

Here is the project(don't have my typed up file, its on a different computer I wont have access to until tomorrow, so I'll wing it)Description:Take the character name, race, rank, gender, class, level, kills, clan and computer the data depending on the request.Example Database Entry:"Frodo" "Tinker gnome" "Advisor" "Transmuter" "Male" "51" "10" "Red Robe" The purpose is to calculate things like: Average kills by race, or Average kills by class, Average level by class, Average kills by gender, etc etc etc.I was originally not going to make this information viewable on a webpage, but I think it'll be better to do so. Excel "can" do this, but I hate the way excel is setup, and was trying to get it done with php since I can handle all the fields better. Excel, I tend to get lost and have a horrible time sorting the data. Also, the information I am using I get from a database that is already in a spreadsheet format, which is why I'm trying to get the csv convertion thing. Copy/paste from website to excel, then save as csv to upload or insert into database, then do calc's.So thats what I'm doing right now.note: I do not have access to the original database, or this wouldn't be a problem in the first place

Link to comment
Share on other sites

I've used something like this to read a regular .xls file:http://devzone.zend.com/article/3336-Readi...sheets-with-PHPI think you were misunderstanding what people were saying to do with fgetcsv though. You don't need to use a database, this doesn't have anything to do with a database. You can use fgetcsv to read the CSV file and start displaying the data on the page, or store the entire data set into some data structure that you would do calculations over, or whatever. You don't have to import the data into a database to use it, you can just use the file. So you can either use the regular .xls files and use a class like the excelreader class to get a collection of worksheets in the file, the rows, columns, etc, or you can parse up a CSV file with fgetcsv and save everything into an array or something like that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...