Aw: Re: upload and import csv to mysql
Hi elogic, since I did the CSV Import for a customer, I can not provide any code examples, so I'm trying to explain the process a little more detailed. I can not guarantee that the following is absolutely MVC conform or that there are no better ways to do this! Assumption: A Customer wants to import his employee data into an already existing application. In the application we have got an employee model, we are going to use for this. For avoiding code repitition, we will use a behavior being able to handle uploads and imports. To create a behavior, the following chapter from the cakebook should be read: http://book.cakephp.org/view/1071/Behaviors Additionally, you can look at one of the behaviors from the bakery. The upload then can be started from any view, by creating a form of type 'file': Form->create('Model', array('action' => 'add/create/whatever', 'type' => 'file', 'name' => 'name')); ?> // Some fields, like caption for an Image or other stuff. Whatever you like ;-) Form->end(__('Submit', true));?> On clicking the submit Button, the action of this form is invoked. That is also the part, we need to handle the upload ourselfs. The Fileinformation are stored in our submitted array, called 'name'. There are two php methods we need for our behavior first: *is_uploaded_file* ( string $filename ) ==> http://de3.php.net/manual/de/function.is-uploaded-file.php *move_uploaded_file* ( string $filename , string $destination ) ==> http://de3.php.net/manual/de/function.move-uploaded-file.php You just use is_uploaded_file to check, and in case its true, you move the uploaded file to your designated location. But since you got your array ready and you are using that little comfortable Framework, you could also use the Cakephp-native FileClass for this: http://api13.cakephp.org/class/file In that case, you would create a file, check for existence and move it to your preferred destination. Don't forget to check the filename and eventually rename it, so no other file gets overwritten. We now got our CSV File on the server and are ready to import that stuff. The way to start your import is totally free of choice. You can either poll a directory for new files and start an import if so, or you could start it manually. The steps needed for import stay the same. 1. Create a new File Object 2. Open it and read the complete content -- Here might start a for loop -- 3. Use a function like *str_getcsv *to parse a single line 4. Create a new Object from your Model 5. Assign the fields from your Array 6. Save the Object to your database -- Here might end a for loop -- These are the basic steps - at least if I haven't forgotten some - and I hope it helps. Anyways, you should read the Cakebook first. It's okay, to glance through the chapters and read them afterwards, when really needed, but you should know what actually is possible and how it can be achieved. Kind regards, Vulinux -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: upload and import csv to mysql
Thankyou for your reply. I only started cakePHP a week or so ago and am struggling a bit. I really haven't found a sample / tutorial online that shows and works with what I need it too. Any direction you could point me in would be appreciated. Thanks again.. On Jul 6, 4:53 pm, Vulinux wrote: > Hi elogic, > > where exactly is your error or aren't you able to find a concept for > your import? > > The way I did this once: Build a Behavior that is able to build an > Object from the imported Csv Data (File Class is your friend). Then > Import the data, call the Csv to Object method and save it to the > database. > > Kind regards, > Vulinux > > On 6 Jul., 04:09, elogic wrote: > > > > > > > > > Hi All, > > > I have setup a file upload feature for my application using the below > > plugin. Now that I have the csv uploading using an upload controller > > and model I now need to insert the csv into a mysql table. I haven't > > had much luck with the samples I have found online. Does anyone know > > how I can do this? I can get my client to name the fields however it > > needs to be done so that isn't an issue. > > > MY UPLOADS_CONTROLLER > > - > > class UploadsController extends AppController > > { > > > var $name = 'Uploads'; > > > // CSV UPLOAD > > // > > -- > > > function upload() { > > > if (!empty($this->data)) > > { > > $this->Upload->create(); > > if($this->Upload->save($this->data)) > > { > > $this->Session->setFlash(__('The > > file has been saved', true)); > > //print_r($this->data); > > $thefilename = > > $this->data['Upload']['file']['name']; > > echo $thefilename; // the uploaded > > CSV FILE NAME! > > > // TO DO PROCESS UPLOADED FILE INTO > > PROPERTIES DATABSE > > } > > else > > { > > $this->Session->setFlash(__('The > > file could not be saved. Please, > > try again.', true)); > > } > > > } > > } > > > } > > > > > > MY UPLOADS MODEL > > - > > > class Upload extends AppModel { > > var $name = 'Upload'; > > var $actsAs = array( > > 'FileUpload.FileUpload' => array( > > 'uploadDir' => 'files', > > 'forceWebroot' => true, //if false, uploads will be saved to > > > > //the uploadDir as a direct path. > > > > //default: true > > /* > > 'fields' => array( > > 'name' => 'file_name', > > 'type' => 'file_type', > > 'size' => 'file_size' > > ), > > */ > > 'allowedTypes' => array( > > 'csv' => array('text/csv', 'application/csv') > > ), > > 'maxFileSize' => '100', //in bytes > > 'unique' => false, //uploaded files will overwrite existing > > files > > 'fileNameFunction' => false, //execute sha1 on fileName if > > required, > > not though > > ) > > );} > > > -- > > > Thanks > > > FILE UPLOAD > > PLUGIN:http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: upload and import csv to mysql
Yes i too need solution for this issue On 6 Jul 2011 07:39, "elogic" wrote: > > Hi All, > > I have setup a file upload feature for my application using the below > plugin. Now that I have the csv uploading using an upload controller > and model I now need to insert the csv into a mysql table. I haven't > had much luck with the samples I have found online. Does anyone know > how I can do this? I can get my client to name the fields however it > needs to be done so that isn't an issue. > > > MY UPLOADS_CONTROLLER > - > class UploadsController extends AppController > { > >var $name = 'Uploads'; > >// CSV UPLOAD >// -- > > >function upload() { > >if (!empty($this->data)) >{ >$this->Upload->create(); >if($this->Upload->save($this->data)) >{ >$this->Session->setFlash(__('The file has been saved', true)); >//print_r($this->data); >$thefilename = $this->data['Upload']['file']['name']; >echo $thefilename; // the uploaded CSV FILE NAME! > > >// TO DO PROCESS UPLOADED FILE INTO PROPERTIES DATABSE >} >else >{ >$this->Session->setFlash(__('The file could not be saved. Please, > try again.', true)); >} > >} >} > > } > > > MY UPLOADS MODEL > - > class Upload extends AppModel { > var $name = 'Upload'; > var $actsAs = array( > 'FileUpload.FileUpload' => array( >'uploadDir' => 'files', >'forceWebroot' => true, //if false, uploads will be saved to > //the uploadDir as a direct path. > //default: true >/* >'fields' => array( > 'name' => 'file_name', > 'type' => 'file_type', > 'size' => 'file_size' >), >*/ >'allowedTypes' => array( > 'csv' => array('text/csv', 'application/csv') >), >'maxFileSize' => '100', //in bytes >'unique' => false, //uploaded files will overwrite existing files >'fileNameFunction' => false, //execute sha1 on fileName if required, > not though > ) >); > } > -- > > > Thanks > > > > > FILE UPLOAD PLUGIN: http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin > > -- > Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org > Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: upload and import csv to mysql
Hi elogic, where exactly is your error or aren't you able to find a concept for your import? The way I did this once: Build a Behavior that is able to build an Object from the imported Csv Data (File Class is your friend). Then Import the data, call the Csv to Object method and save it to the database. Kind regards, Vulinux On 6 Jul., 04:09, elogic wrote: > Hi All, > > I have setup a file upload feature for my application using the below > plugin. Now that I have the csv uploading using an upload controller > and model I now need to insert the csv into a mysql table. I haven't > had much luck with the samples I have found online. Does anyone know > how I can do this? I can get my client to name the fields however it > needs to be done so that isn't an issue. > > MY UPLOADS_CONTROLLER > - > class UploadsController extends AppController > { > > var $name = 'Uploads'; > > // CSV UPLOAD > // -- > > function upload() { > > if (!empty($this->data)) > { > $this->Upload->create(); > if($this->Upload->save($this->data)) > { > $this->Session->setFlash(__('The file > has been saved', true)); > //print_r($this->data); > $thefilename = > $this->data['Upload']['file']['name']; > echo $thefilename; // the uploaded > CSV FILE NAME! > > // TO DO PROCESS UPLOADED FILE INTO > PROPERTIES DATABSE > } > else > { > $this->Session->setFlash(__('The file > could not be saved. Please, > try again.', true)); > } > > } > } > > } > > > > MY UPLOADS MODEL > - > class Upload extends AppModel { > var $name = 'Upload'; > var $actsAs = array( > 'FileUpload.FileUpload' => array( > 'uploadDir' => 'files', > 'forceWebroot' => true, //if false, uploads will be saved to > > //the uploadDir as a direct path. > > //default: true > /* > 'fields' => array( > 'name' => 'file_name', > 'type' => 'file_type', > 'size' => 'file_size' > ), > */ > 'allowedTypes' => array( > 'csv' => array('text/csv', 'application/csv') > ), > 'maxFileSize' => '100', //in bytes > 'unique' => false, //uploaded files will overwrite existing > files > 'fileNameFunction' => false, //execute sha1 on fileName if > required, > not though > ) > );} > > -- > > Thanks > > FILE UPLOAD > PLUGIN:http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
upload and import csv to mysql
Hi All, I have setup a file upload feature for my application using the below plugin. Now that I have the csv uploading using an upload controller and model I now need to insert the csv into a mysql table. I haven't had much luck with the samples I have found online. Does anyone know how I can do this? I can get my client to name the fields however it needs to be done so that isn't an issue. MY UPLOADS_CONTROLLER - class UploadsController extends AppController { var $name = 'Uploads'; // CSV UPLOAD // -- > function upload() { if (!empty($this->data)) { $this->Upload->create(); if($this->Upload->save($this->data)) { $this->Session->setFlash(__('The file has been saved', true)); //print_r($this->data); $thefilename = $this->data['Upload']['file']['name']; echo $thefilename; // the uploaded CSV FILE NAME! // TO DO PROCESS UPLOADED FILE INTO PROPERTIES DATABSE } else { $this->Session->setFlash(__('The file could not be saved. Please, try again.', true)); } } } } MY UPLOADS MODEL - array( 'uploadDir' => 'files', 'forceWebroot' => true, //if false, uploads will be saved to //the uploadDir as a direct path. //default: true /* 'fields' => array( 'name' => 'file_name', 'type' => 'file_type', 'size' => 'file_size' ), */ 'allowedTypes' => array( 'csv' => array('text/csv', 'application/csv') ), 'maxFileSize' => '100', //in bytes 'unique' => false, //uploaded files will overwrite existing files 'fileNameFunction' => false, //execute sha1 on fileName if required, not though ) ); } -- Thanks FILE UPLOAD PLUGIN: http://www.webtechnick.com/blogs/view/221/CakePHP_File_Upload_Plugin -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php