Upload .csv file to AS400 PF

General discussion on Zend Core for IBM System i
Post Reply
rcv400
Posts: 4
Joined: Tue Mar 31, 2009 3:05 pm

Upload .csv file to AS400 PF

Post by rcv400 » Fri Apr 03, 2009 1:30 pm

Hi!

I would like to upload a .csv file to a PF in the AS400. The PF is made up of 4 fields of equal length. Normally, the upload is done via IBM client access using the .fdf file. But now I'm looking for a PHP alternative.

My first option was to upload the .csv file to the IFS, then run a CL command to move the records to the PF.

Is there a more direct way to do the upload using PHP/i5 API's?

User avatar
rodflohr
Zend Global Support
Posts: 56
Joined: Mon Dec 29, 2008 5:28 pm

Re: Upload .csv file to AS400 PF

Post by rodflohr » Sat Apr 04, 2009 1:24 am

One way would be to use a POST method upload to upload the file. ( http://us.php.net/manual/en/features.fi ... method.php ) Google turns up lots of examples. This one looks pretty easy to follow: http://www.htmlgoodies.com/beyond/php/a ... hp/3472551

Once you have the file uploaded, you can use a CL command (like CPYFRMIMPF) to pull the data into your PF.

rcv400
Posts: 4
Joined: Tue Mar 31, 2009 3:05 pm

Re: Upload .csv file to AS400 PF

Post by rcv400 » Sat Apr 04, 2009 1:35 am

thanks for the suggestion. i tried this out and it's ok. however, is there a way to just read the .csv file per line, and then transfer these records directly to the PF using any of the i5 PHP API's? i want to do away with the IFS and the CL command.

User avatar
rodflohr
Zend Global Support
Posts: 56
Joined: Mon Dec 29, 2008 5:28 pm

Re: Upload .csv file to AS400 PF

Post by rodflohr » Mon Apr 06, 2009 6:31 pm

You can add a record to your PF using i5_new_record(), which takes an array as input. To do this you need to parse out each line of your csv file to be an array of values to load the record.

There are a couple of ways to do it. One way is to read the entire csv into a string using file_get_contents(), and then parse the string using str_getcsv(). Another way to do it is to use fopen() and fgetcsv() to read the input file one line at a time and pull the elements into an array.

If you can read the file from your i5 using ftp, then you can specify the ftp location in the file_get_contents() or fopen() function. It would look something like this:

$handle = fopen("ftp://user:password@example.com/somefile.txt", "r");

Post Reply