Edit Flatfile with PHP

I want to use php to replace data in a flatfile. (sorry about the format below, just paste to notepad for clarity):
my flat file is foramted as a CSV with the 'pipe' as the delimiter instead of a comma
each row is a 'record' with unique record numbers, "ID"
the rowdata is identified by column names at the top.  Total file is 1600 lines long.

|ID|Item|Qty|Cate|Price4|Price1|Price5|Price6|Price7|Out|Due|Name|Total|avail|image|safety|
|2101|12 ft Utility Trailer|1|Trailer||30.00 |75.00 |45.00 |150.00 |||||Yes|Image2101.png|2101_S.pdf|
|2102|16 ft Utility Trailer|1|Trailer||35.00 |87.50 |52.50 |175.00 |||||Yes|Image2102.png|2101_S.pdf|
|2103|2 Person Tent|1|Camping||6.00 |15.00 |9.00 |30.00 |||||Yes|Image2103.png||
|2104|2 Person Tent|1|Camping||6.00 |15.00 |9.00 |30.00 |||||Yes|Image2104.png||
|2105|24ft -29ft Travel Trailer|1|Camper||70.00 |175.00 |105.00 |350.00 |||||Yes|Image2105.png|2105_S.pdf|

etc...

I want to update the the 'avail' colmun  with "NO", the 'Name' column with a "name" and the Out and Due cols with a date
 (the 'change' data is gathered from a form passed to the script by using unique record number 'ID').

I have used the following in the past, but does not work withg my hosting provider now.

$arg = "http://www.mysite.com/cgi-bin/RentUpdate1.pl?ID=$ID_1&action=save&password=''&avail=No&Name=$LName&Due=$DueItemA";
require ($arg);

 I would appreciate any direction, I prefer to do it from same php script.
tomrectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
<?php
// The filenames we'll be dealing with
$inFile = "original.csv";
$outFile = "updated.csv";

// Open the file handles - read the original and write the updated file
$fpIn = fopen($inFile,"r");
$fpOut = fopen($outFile,"w");

// Read the header line and copy it to the new file
$header = fgetcsv($fpIn,0,'|');
fputcsv($fpOut,$header,'|');

// Find the index of the avail column
$idxAvail = array_search("avail",$header);

// Read all the records
while(!feof($fpIn))
{
  // Read in a pipe-delimited record into an array
  $record = fgetcsv($fpIn,0,'|');

  // Update the "avail" column to "No" for item ID 2103
  if($record[0] == "2103")
  {
    $record[$idxAvail] = "No";
  }

  // Write the updated record out to the new file
  fputcsv($fpOut,$record,'|');
}

// Close the file handles
fclose($fpIn);
fclose($fpOut);
?>

Open in new window

0
Ray PaseurCommented:
... but does not work with my hosting provider...
How does this not work?  Does it cause a fire, or make the machine tip over, or scares the cat?  Or maybe it creates an error message of some sort?  If there is a message, we might be able to help with that part ;-)
0
tomrectorAuthor Commented:
gr8gonzo,  thanks for your support,,,,
 error:
Warning: fputcsv() expects parameter 2 to be array, boolean given in /hermes/waloraweb096/b500/moo.thrsevfi/misc/edit.php on line 30


Ray
    for the php 'require', The hosting provifder I use now has 'require'  disabled at server level,  adding 'allow_url_fopen = ON ' to php.ini does not change anything
0
gr8gonzoConsultantCommented:
Your hosting provider has disabled "require" ???? Are you 100% certain about that? I could understand if they disabled url_fopen, but I've NEVER seen a provider disable something as essential as require, especially since it's not a risk of any sort.

If the only problem is connectivity via the URL wrapper, you can always hit the URL with cURL...

If you're getting a boolean for the second parameter, then it sounds like fgetcsv is not returning the expected data. You could also try changing this out:

Old:
// Read all the records
while(!feof($fpIn))
{
  // Read in a pipe-delimited record into an array
  $record = fgetcsv($fpIn,0,'|');

  // Update the "avail" column to "No" for item ID 2103
  if($record[0] == "2103")
  {
    $record[$idxAvail] = "No";
  }

  // Write the updated record out to the new file
  fputcsv($fpOut,$record,'|');
}

Open in new window

New:
// Read in a pipe-delimited record into an array
while (($record = fgetcsv($fpIn, 0, '|')) !== false) 
{
  // Update the "avail" column to "No" for item ID 2103
  if($record[0] == "2103")
  {
    $record[$idxAvail] = "No";
  }

  // Write the updated record out to the new file
  fputcsv($fpOut,$record,'|');
}

Open in new window

0
Ray PaseurCommented:
I didn't even think it was possible to disable require().  Perhaps you can disable require() when a URL wrapper is used, but this makes no sense to me either.  I would suggest you get a different hosting provider.  Go to the "thanks to" on the php.net home page and choose one of those providers.

Suggest you also add error_reporting(E_ALL) to the top of the script.  By default, PHP will suppress the Notice messages, and you do not want those messages suppressed!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.