eco Posted July 27, 2010 Share Posted July 27, 2010 Hi all, It's been a while since I was here. It's been a while since I got a chance to get back to php seriously come to think of it. I'm a newb though so be gentle I'm trying to learn the language by writing a basic application that will read from .csv, store in db and write a .qif file to import into my accounting program. Correct me if I'm wrong but I discovered you can't call a function from an html for so I did the following but can't get it to work. I suspect I'm not sending the correct info to the function. Here is what I've come up with: index.php <?php error_reporting ( E_ALL ); include_once 'csvtest.php'; <form method="post" enctype="multipart/form-data"><label for="file">Filename:</label> <input type="file" name="csvfile" id="csvfile" /><br /> <input type="submit" name="submit" value="Submit" /></form> <?php /* * Validate the file */ if ($_FILES ["csvfile"] > 0) { $csvfile = $_FILES ["csvfile"] ["name"]; if ($csvfile != "kbc.csv" && $csvfile != "kt.csv") { die ( "<p>The csv file can only be named <b>kbc.csv</b> or <b>kt.csv</b>.</p>" ); } } else { die ( "<br/><b>Error: Can't find the csv file (" . $_FILES ["csvfile"] ["error"] . ").</b><br/>" ); } // Send data to function $csv = new csvtest (); $csvarray = $csv->csv2array ( $csvfile ); echo "count: " . $csvarray[0]; ?> csvtest.php <?php class csvtest { public function csv2array($csvfile) { echo "In csv2array function"; $arrResult = array (); $handle = fopen ( $csvfile, "r" ); if ($handle) { while ( ($data = fgetcsv ( $handle, 1000, ";" )) !== FALSE ) { $arrResult [] = $data; } fclose ( $handle ); } else { die ( "<br/><b>Error: Can't open the csv file: $csvfile</b><br/>" ); } //return $arrResult; return count($arrResult); } } What am I doing wrong and more importantly, am I going at it the right way? Many thanks for any advice! Quote Link to comment https://forums.phpfreaks.com/topic/209018-form-file-to-a-function/ Share on other sites More sharing options...
AbraCadaver Posted July 27, 2010 Share Posted July 27, 2010 http://us3.php.net/manual/en/features.file-upload.post-method.php I didn't look through all of the code so their may be other issues, but you need to use: $_FILES ["csvfile"] ["tmp_name"] That is the file that has been uploaded to the server. Quote Link to comment https://forums.phpfreaks.com/topic/209018-form-file-to-a-function/#findComment-1091857 Share on other sites More sharing options...
eco Posted July 27, 2010 Author Share Posted July 27, 2010 Hi AbraCadaver, Thanks for your post. I read the article and tried to find a solution all this evening without success. It seems I still have a bit more to learn before I can do this properly so it's back to the old If you do however have the time for a basic example of a file form being sent to a function (in a seperate file) that would be fab. Quote Link to comment https://forums.phpfreaks.com/topic/209018-form-file-to-a-function/#findComment-1091875 Share on other sites More sharing options...
jcbones Posted July 27, 2010 Share Posted July 27, 2010 This should get you started. <?php function uploadFile($name) { foreach($_FILES[$name] as $k => $v) { echo '<br />' . $k . ': ' . $v; } return; } if(isset($_POST['submit'])) { uploadFile('file'); } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" name="submit" value="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/209018-form-file-to-a-function/#findComment-1091884 Share on other sites More sharing options...
eco Posted July 28, 2010 Author Share Posted July 28, 2010 Hi jcbones, Thanks for your post. I'll get working on it this evening when I get back home. I looks to me like all this fits in one file though. I'm trying to send the 'file to a seperate php file with a class->function that will deal with the uploaded file. For example the index.php has a form that loads the file and sends it to csv.php which has a class/function that will put the file into an array and send the array back to index.php something like this: index.php include_once 'csvtest.php'; <form method="post" action="" enctype="multipart/form-data"><label for="file">Filename:</label> <input type="file" name="csvfile" id="csvfile" /><br /> <input type="submit" name="submit" value="Submit" /></form> <?php if ($_FILES ["csvfile"] > 0) { // Send data to function $csv = new csvtest (); $csvarray = $csv->csv2array ( $csvfile ); } foreach($csvarray as $k => $v) { echo '<br />' . $k . ': ' . $v; } csv.php <?php class csvtest { public function csv2array($csvfile) { echo "In csv2array function"; $arrResult = array (); // open the file $handle = fopen ( $csvfile, "r" ); if ($handle) { // put csv file into array while ( ($data = fgetcsv ( $handle, 1000, ";" )) !== FALSE ) { $arrResult [] = $data; } fclose ( $handle ); } else { die ( "<br/><b>Error: Can't open the csv file: $csvfile</b><br/>" ); } //return $arrResult to the index.php file return count($arrResult); } } This is the way I see it but (a) it doesn't work and (b) I'm not sure the design is so good. I'm just trying to move the processing from the main file. Your thoughts? is a good answer too. Quote Link to comment https://forums.phpfreaks.com/topic/209018-form-file-to-a-function/#findComment-1091961 Share on other sites More sharing options...
eco Posted July 29, 2010 Author Share Posted July 29, 2010 Hi, I just thought I'd let everyone who helped that I did get it working in the end. I did however compromise a bit and drop my will to use oop and simply put most of the data into the one file. I'll just do some more reading before updating my code to oop. I also had a good browse on the site and found some nice debugging info that helped solve some of my problems. In sort, thank you all for your help. I'll be back soon. Quote Link to comment https://forums.phpfreaks.com/topic/209018-form-file-to-a-function/#findComment-1092469 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.