Jump to content

form 'file' to a function


eco

Recommended Posts

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!

Link to comment
Share on other sites

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  :rtfm:

 

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.  ;)

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?  :rtfm: is a good answer too.  ;)

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.