/    Sign up×
Community /Pin to ProfileBookmark

i want a form to write to a file but they can only change some items in the form here is the example format

name=one form changes this
link=another form changes this
desc=another form changes this
dir=this one just writes dir= so they cant change it
file=this one should = launcher.exe and they cant change it

it must write the thing before the values without them having to type it in example they type in bleh in the first form it writes name=bleh to the file addons.list
i tried a couple of times but cant figure out how to do it

[COLOR=DarkRed]NOTE:im not very good with php[/COLOR]

thanks ?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisOct 22.2006 — What kind of file are you writing to? Myself, when I can't use a database, I use a CSV file instead. Basically, I read the entire file into memory (a 2D array) and update that. When I'm ready to save it, I re-write the entire CSV file.

This function reads in the CSV file:
[code=php]function csv_capture($file, $maxlen) // capture CSV file into 2-d array
{
$array = array(); // create empty array
$done = false; // set completion flag off
$fp = @fopen($file, "r"); // open master CSV file as input
if($fp && !feof($fp)): // if file opened successfully
while($temp = fgetcsv($fp, $maxlen)): // while file record returned
$array[] = $temp; // add to 2-d array
endwhile;
if(feof($fp)) $done = true; // if at end, set completion flag on
endif;
if($fp) fclose($fp); // if valid file handle, close it
if($done) return $array; // return 2-d array result
return false; // else signal error has occurred
}[/code]

This function re-writes the entire CSV file:
[code=php]function csv_replace($file, $array) // replace CSV file from 2-d array
{
$copy = $file.'.tmp'; // create temporary CSV file name
$done = false; // set completion flag
$fp = fopen($copy, "w"); // open temporary CSV file as output
if($fp):
$done = true; // set completion flag
foreach($array as $data): // write each sub array element
if(!fputcsv($fp, $data)): // as CSV record output
$done = false; // on failure reset completion flag
break; // exit foreach construct
endif;
endforeach;
fclose($fp); // close temporary CSV file
endif;
if($done): // if completed successfully
@unlink($file); // delete primary file
return rename($copy, $file); // rename temporary file as primary
endif;
return false; // else signal error has occurred
}[/code]

This is the function called from that one:
[code=php]function fputcsv($fp, $array, $delim=',') // write 1-d array as CSV record
{
switch(strlen($delim)): // edit field delimiter length and content
case 0; // empty string
$delim = ','; // defaults to comma delimiter
break; // exit switch construct
case 1; // only delimiter length of 1 supported
break; // exit switch construct
default; // delimiter too long
$delim = str_left($delim, 1); // truncate delimiter to 1 character
endswitch;
if ($delim <= ' ' || '~' <= $delim): // if delimiter not in range
$delim = ','; // defaults to comma delimiter
endif;
//
$line = ''; // create formatted CSV record
foreach($array as $val): // format each array value
if(is_numeric($val)): // numeric values
$line .= $val . $delim; // go in without value delimiters
else: // string values
$val = str_replace("rn", "n", $val); // change all cr/lf to lf only
$val = str_replace('"', '""', $val); // double up any double-quotes
$line .= '"' . $val . '"' . $delim; // and the result goes in double-quotes
endif;
endforeach;
$line = str_left($line, strlen($line) - 1); // remove last field delimiter
return fwrite($fp, $line . "n"); // write record to CSV file
}[/code]
×

Success!

Help @Tredan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...