Page 1 of 1
Write to a txt file?
Posted: Thu Jan 30, 2003 10:02 am
by slipstream
What is the code to write to a txt file?
I am using Flash as the interface, I create the file like this?
if (!file_exists($filename))
{
$file=fopen($filename, "w");
fclose ($file);
}
How do I write to the file, I don't think it is even creating it.
Posted: Thu Jan 30, 2003 12:05 pm
by lazy_yogi
it may not create it if you don't use fwrite
when i create files like that, I usually have in the below area $input = ""; which writes nothing, but the file is created.
this is how to write to files. To append to the end of a file, use 'a' instead of 'w'in fopen.
Code: Select all
<?php
if ($f = fopen("messages.txt", 'w')) {
fwrite($f, "$input");
fclose($f);
}
?>
thanks
Posted: Thu Jan 30, 2003 12:09 pm
by slipstream
Yep, it works now. I also am writing to a file but now I need to know something else.
Lets say I write these records to a file:
Me 10 01/20/03 Worked on a project
you 8 01/20/03 Worked on nothing
No I want to read it so that it does int an array of records so the first record would have these values:
name = Me
hours = 10
date = 01/20/03
comment = Worked on a project
How do I do this, should I use a delimiter? How to I make and put values into the array? Thanks
Posted: Thu Jan 30, 2003 7:00 pm
by lazy_yogi
Code: Select all
<?php
<?
print "<html><body>";
$f = file("file.txt"); // returns an array with each line in a new array pos
foreach($f as $line) {
$elements = preg_split('/\s+/',$line);
$name = $elements[0];
$hours = $elements[1];
$date = $elements[2];
$line = preg_replace('/^\S+\s+\S+\s+\S+\s+/',"",$line);
$comment = $line;
// now you can access $name,$hours,$date,$comment
print "name=$name<BR>hours=$hours<BR>date=$date<BR>comment=$comment<BR><BR>";
}
?>
?>
search for "preg_replace" and "preg_split" on php.net and play around with them to understand how they work. They user regex (regular expressions) .. u might want to learn them.
Posted: Fri Jan 31, 2003 3:33 am
by twigletmac
It'd probably be easiest to use a delimiter such as a pipe (|) in your text file because then you can just do the following to get the info out:
Code: Select all
$file = file('file.txt'); // returns an array with each line in a new array pos
foreach($file as $line) {
$info = explode('|', $line);
echo 'name = '.$info[0].'<br />';
echo 'hours = '.$info[1].'<br />';
echo 'date = '.$info[2].'<br />';
echo 'comments = '.$info[3].'<br />';
}
Mac
thanks
Posted: Fri Jan 31, 2003 10:13 am
by slipstream
Hey thanks,
that helps me get started a lot. I now read the file fine but if the users select the info from combo boxes (like name and hours) how would I enter that at the end of the file separated by the pipe delimiter?
Thanks again..
Posted: Fri Jan 31, 2003 1:21 pm
by slipstream
come on guys, some one must know..
Posted: Fri Jan 31, 2003 2:02 pm
by bionicdonkey
open the file for append
$f = fopen("file.txt", "a");
that puts the file pointer at the end of the file
thanks
Posted: Fri Jan 31, 2003 2:14 pm
by slipstream
but that wasn't my question, please read the entire thread.
Posted: Fri Jan 31, 2003 3:25 pm
by puckeye
When you create your string to write with fwrite() you'll have to use pipes(|) instead of spaces. That's all.
But since all the values are probably coming from a form like you stipulated you only have to create a string with the first variable then add a pipe then another variable then a pipe etc...
Code: Select all
<?php
$work_str = $name."|".$hours."|".$date."|".$comments;
?>
Then use $work_str with fwrite...
thanks
Posted: Fri Jan 31, 2003 3:27 pm
by slipstream
That helped a lot , got it now..