Getting filename through $_POST doesnt work

  php

I made a simple input text in HTML and now i want to create a file in the php script that gets the name from the input through $_POST.

Thats the HTML:

<html>
    <header>
        <title>Test</title>
    </header>
    
    <body>
        <form action="test.php" method="post">
            <p>Website Name: <input type="text" name="websiteName" /></p>
            <p>Meinung: <input type="text" name="websiteOp" /></p>
            <p><input type="submit" value="Abschicken" /></p>
        </form>
    </body>
</html>

And thats the PHP:

<?php
    $websiteName = $_POST['websiteName'];
    $websiteOp = $_POST['websiteOp'];
    
    echo $websiteName . PHP_EOL;
    echo $websiteOp . PHP_EOL;
    echo "###########" . PHP_EOL;
    
    function checkForSite() {
        if( strpos(file_get_contents("websites.txt"),$websiteName) != false){
            echo PHP_EOL . "Website existiert schon! Erstelle Kommentar!" . PHP_EOL;
            $newFile = fopen('websites/' . $websiteName . '.txt', "a+");
            fwrite($newFile, $websiteOp);
            fclose($newFile);
        }
        else
        {
            createSiteEntry();
        }
    }
    
    function createSiteEntry() {
        $newFile = fopen('websites/' . $_POST['websiteName'] . '.txt', "a+");
        fwrite($newFile, $_POST['websiteOp']);
        fclose($newFile);
        echo "Eintrag erstellt und mit Meinung versehen!";
    }
    
    checkForSite();
    
    
?>

Now im not getting any file. With some other tries before i always got a ".txt" file without content.

Source: Ask PHP

LEAVE A COMMENT