soheils
soheils

Reputation: 53

fwrite function writes nothing to the file


I am trying to write data into a txt file, but It won't work. there is no error. it just won't work.
here is the code

if (isset($_REQUEST['submit']))
    {
        $hour = $_REQUEST['hour'];
        $family = $_REQUEST['family'];
        $how = $_REQUEST['how'];
        $will = $_REQUEST['will'];
        $fun = $_REQUEST['fun'];
        $kind = $_REQUEST['kind'];
        $device = $_REQUEST['device'];
        $study = $_REQUEST['study'];
        $agree = $_REQUEST['agree'];
        $text = "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />" ;
        fopen('/1.txt', "r");
        fwrite('/1.txt',"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
        fclose('/1.txt');

    }

do note that there is an "else" that includes all the aforementioned input fields that I used in the code , and that I am executing on localhost. thanks

Upvotes: 1

Views: 827

Answers (4)

Akshay Kalose
Akshay Kalose

Reputation: 777

You cannot write to it since you are opening it as read-only:

$handle = fopen('/1.txt', "r");

Instead:

$handle = fopen('/1.txt', "w"); // to write only, if you need to read and write use 'w+'

You also need to store fopen in a $handle so you can write to it later.

Documentation: http://www.php.net/manual/en/function.fopen.php

Now the first parameter for fwrite should be $handle:

fwrite($handle, "hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");

Documentation: http://www.php.net/manual/en/function.fwrite.php

At the end you should also close with $handle:

fclose($handle);

Documentation: http://www.php.net/manual/en/function.fclose.php

Upvotes: 6

AbraCadaver
AbraCadaver

Reputation: 78994

Much simpler:

file_put_contents('/1.txt', $text);

Upvotes: 0

Abdullah Bakhsh
Abdullah Bakhsh

Reputation: 678

You have to pass in a 'w' to write to a file.

$handle = fopen('/1.txt', "w");

Passing in 'r' makes it so you are opening a 'read-only' file.

Upvotes: 0

idmean
idmean

Reputation: 14875

You are opening the file as read-only, with the ruse w instead. And you need to pass fwrite the return of fopen as first argument, not the filename:

$f = fopen('/1.txt', "w");
fwrite($f,"hour :" . $hour . "<br />" . "family" . $family . "<br />" . "تطابفق با درس" . $how . "<br />" . "سرگرمی های غیر مجازی : " . $will . "<br />" . "نوع سرگرمی :" . $fun . "<br />" . "kind :  " . $kind . "<br />" . "device " . $device . "<br />" . "اثیر بر درس" . $study . "<br />" . "کنترل " . $agree . "<br />");
fclose($f);

Upvotes: 2

Related Questions