Write to a txt file?
Moderator: General Moderators
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
Write to a txt file?
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.
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.
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.
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);
}
?>-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
thanks
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
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
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>";
}
?>
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
Mac
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 />';
}-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
thanks
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..
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..
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
-
bionicdonkey
- Forum Contributor
- Posts: 132
- Joined: Fri Jan 31, 2003 2:28 am
- Location: Sydney, Australia
- Contact:
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
thanks
but that wasn't my question, please read the entire thread.
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
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...
Then use $work_str with fwrite...
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;
?>-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
thanks
That helped a lot , got it now..