Write to a txt file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Write to a txt file?

Post 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.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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);
}
?>
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

thanks

Post 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
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

thanks

Post 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..
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

Post by slipstream »

come on guys, some one must know..
bionicdonkey
Forum Contributor
Posts: 132
Joined: Fri Jan 31, 2003 2:28 am
Location: Sydney, Australia
Contact:

Post by bionicdonkey »

open the file for append

$f = fopen("file.txt", "a");

that puts the file pointer at the end of the file
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

thanks

Post by slipstream »

but that wasn't my question, please read the entire thread.
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post 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...
slipstream
Forum Commoner
Posts: 86
Joined: Fri Apr 19, 2002 8:53 am
Location: Canada

thanks

Post by slipstream »

That helped a lot , got it now..
Post Reply