trying to write to a file with php

351 views Asked by At

I am trying to write to a file using php and html, I am running a XAMPP local server on my computer I a wrote some html and php code I found online on YouTube, I am also on Mac just to let people know.

Here is my html:

<html>
<head>
    <title>testinf file I/O</title>
    </head>
    <body>
        <form action="data.php" method="post">name:
            <input type="text" name="name">
            address:<textarea name="adress"></textarea>
            email: <input type="email" name="email">
            <input type = "submit"name="btnl">
        </form>
    </body>
</html>

And here is my PHP:

<html>
<head>
    <title>store data to file
    </title>
    </head>
    <body>
        <?php 
        $name=$_POST['name'];
        $address=$_POST['address'];
        $email=$_POST['email'];
        $str="name is".$name."address is".$address."email is".$email;
        
        $fp=fopen("B.txt","w");
        fwrite($fp,$str);
        fclose($fp);
        
        echo"conctent is stored in the B.txt file";
        
        ?>
    </body>
</html>

when I run the server and go to local host, go to the html page and fill out the form and click submit I then try looking for a txt file with the contents I entered on the forum and I can't find it. I then looked through the logs and found this in the access log:

::1 - - [10/Apr/2023:11:59:59 -0400] "POST /testingI:O/data.php HTTP/1.1" 500 88

I found this in the PHP_error_log:

[10-Apr-2023 17:59:59 Europe/Berlin] PHP Warning:  Undefined array key "address" in /Applications/XAMPP/xamppfiles/htdocs/testingI:O/data.php on line 9
[10-Apr-2023 17:59:59 Europe/Berlin] PHP Warning:  fopen(B.txt): Failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/testingI:O/data.php on line 13
[10-Apr-2023 17:59:59 Europe/Berlin] PHP Fatal error:  Uncaught TypeError: fwrite(): Argument #1 ($stream) must be of type resource, bool given in /Applications/XAMPP/xamppfiles/htdocs/testingI:O/data.php:14
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/testingI:O/data.php(14): fwrite(false, 'name isfwiweijf...')
#1 {main}
  thrown in /Applications/XAMPP/xamppfiles/htdocs/testingI:O/data.php on line 14

if anyone can help me fix this error because ive been trying to write to a file and its just not been working out.

I looked at multiple YouTube videos and they keep on getting a txt output files that will pop up in the same htdocs folder that the php and html code are located.

1

There are 1 answers

0
enesad On

If you try to open a file for which you don't have write permissions using the fopen function, it will return false. And because you are trying to write the file with the fwrite function without checking this return, you are causing a fatal error. Try the following solutions to fix the issues in this script.

Solution(s):

  • When retrieving data from POST in PHP, it is important to pay attention to the names. You must enter the name of the address input in the same way in PHP. To fix this, edit the relevant line in PHP as follows:

    $address = $_POST['adress'];

    Additionally, it's important to check if it exists with the isset function before trying to retrieve the data contained in $_POST. e.g.

    if (isset($_POST['adress'])) {
       $address = $_POST['adress'];
    }
    ...
    
  • Make sure you have the necessary permissions when doing anything with the file system. You need to enable write permissions for the folder:

    /Applications/XAMPP/xamppfiles/htdocs/testingI:O/

    This process can be done in different ways for different operating systems. For Mac, you need to research how it's done.

  • Simplify the file writing process by using the file_put_contents function instead of using the fopen, fwrite, fclose functions. This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.

    file_put_contents('B.txt', $str);