CSV upload, once uploaded to a temp folder overwrite the existing database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: CSV upload, once uploaded to a temp folder overwrite the existing database (/showthread.php?tid=57525) |
CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-20-2013 [eluser]the_unforgiven[/eluser] Hi all, I'm looking for a library or some help on creating a upload form that allows uploads of a CSV file that then gets saved to the database overwriting the data that's already there. This will only be done once a year or twice a year by the client via the CMS i have built for them, so it needs to work like I explained. Is there anyone who can point me in the right direction to create this or is there something already out there? Thanks for reading and I'll look forward to your replies. CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-20-2013 [eluser]davidMC1982[/eluser] Here's some code I've used to do something similar. I'm using Datamaper ORM throughout my app but in this case, it's only specifically used to clear my table data. After that I used the standard CI DB class. I'm also getting my CSV file from an external data source. You'd have to replace this to open the csv file. My code builds one giant INSERT statement. In my case, if the INSERT fails, it has no bearing on the rest of my app. I wouldn't attempt to do this on a table that is critical to the functioning of your site. Instead, I would do the INSERT on a temp table first. Anyway, this should give you an idea of the steps you might need to take. Code: function refresh_rates() { CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]the_unforgiven[/eluser] Cool, that looks straight forward i'll have a go at that see what happens...Thanks dude CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]the_unforgiven[/eluser] Done what you said and i'm getting the following error: Code: Error Number: 1064 CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]TheFuzzy0ne[/eluser] You're not providing any data to insert into the database. Either ensure that the data is there, or add a conditional to make sure the query isn't executed if there's no data to insert. CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]the_unforgiven[/eluser] ok well i have the following and getting the file upload or so i thought. Code: function add() What am i doing wrong? CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]TheFuzzy0ne[/eluser] All you're doing is explode()ing the file name. Since that's only a single line, you're array only has a single line, which you're removing with array_shift. You need to open the file in order to obtain any data from it. The array that $this->upload->data() returns is simply information about the uploaded file, not the file itself. Debug, debug, debug! Check your code each step of the way. Output arrays and strings to make sure they contain the data you think they do. CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]the_unforgiven[/eluser] Great cheers i'll do just that, will i need to store the uploaded file temporary somewhere? CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]TheFuzzy0ne[/eluser] The file is already stored in a temporary directory, and remains there until you move it. You can access it like so: Code: $file_contents = file_get_contents($_FILES['field_name']['tmp_name']); Obviously, field_name should be replaced with the name of your file upload field. CSV upload, once uploaded to a temp folder overwrite the existing database - El Forum - 03-21-2013 [eluser]the_unforgiven[/eluser] Got ya! thanks dude |