Jump to content

*SOLVED* Writing with php


Avendium

Recommended Posts

How would go about taking a value from a form object and printing it on a line that I choose in an external file? for example. If i have the following in my external file, data.txt.

data1
data2
data3
data4

and i want the user to input a value and that value be written on line 2 so that when the submit button is pressed the txt will now show.

data1
(whatever the input was)
data3
data4

thanks
Link to comment
Share on other sites

To retrieve the contents from the file use the file handling functions, being file_get_contents() the easiest in this case.

Then, use str_replace() to replace data2 with the input. Something like:

[code]$data = file_get_contents('data.txt');
$data = str_replace('data2', $_POST['data'], $data);[/code]

[a href=\"http://www.php.net/file_get_contents\" target=\"_blank\"]http://www.php.net/file_get_contents[/a]
[a href=\"http://www.php.net/str_replace\" target=\"_blank\"]http://www.php.net/str_replace[/a]
Link to comment
Share on other sites

hmm, this would work if i knew what "data2" was in order to replace it. The problem is, data 2 could be anything. Is there anyway to just target and replace the whole line without knowing what is there to begin with? and without replacing the first line?

what im trying to do is this:

I have my form in form.php. I want to send the data to my data.txt, on the line designated for that perticular form, in this case its line 2. So whenever the user inputs a new value, line 2 would be replaced with that new input. so i never know what line 2 holds to replace it.

thanks for your help.
Link to comment
Share on other sites

Look at the file() function - [a href=\"http://ca.php.net/manual/en/function.file.php\" target=\"_blank\"]http://ca.php.net/manual/en/function.file.php[/a] - it reads a file into an array. Your 'data2' will be the second element of that array. You can then set the new value POSTed as equal to the second element and write the array back to the file. See the file write function for that.

Remember that the first element of an array is $arr[0] not $arr[1].

Create some code and we'll work with you from what you do.
Link to comment
Share on other sites

I managed to load the file as an array and display the line that i want but im having trouble inputing the value that i get from my form back into the array.

Form code
[code]<form action="test.php" method="POST">
name: <input type="text" name="name" />
<input type="submit" value="Submit"/>
</form>[/code]

PHP code
[code]<?
    $filename = 'test.txt'; //filename
    
        if (file_exists($filename)) {
        $data = file($filename, 'r+'); //Initialize array

        //debug
        echo $data[0];
        
    } else {
           echo "The file $filename does not exist";
    break;
}
?>[/code]

my txt file has the lines

line0
line1
line2
line3

I can display them fine but im having trouble trying to figure out how to place the input back into the file to replace line1 which would be $data[1] in the array. What kind of write function should i use?
Link to comment
Share on other sites

try

[code]if (isset($_POST['text'])) {

    $filename = 'test.txt'; //filename
    
    if (file_exists($filename)) {
        $data = file($filename); //Initialize array

            // replace second line
        $data[1] = $_POST['text'] . "\n";
        
            // rewrite the file
            // ## remove '_' in filesys functions
        $fp = f_open($filename, 'w');    
        foreach ($data as $line) {
            f_write($fp, $line);
        }
        f_close($fp);
        
    } else {
           echo "The file $filename does not exist";
    }
}[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.