Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP text file write issue

gregaryb

New Coder
I have this PHP code to write add a keyword to the end of a robots.txt file.
My web page gets an alert message saying that the keyword was successfully added to robots.txt.
However the contents of the actual robots.txt never changes.
What the hell?
I am at a loss to figure out what is happening here.
Any suggestions?

Code:
    if (isset($_POST["submit_ban_keyword"]))
    {
        /*
            file_put_contents does not work and does not throw an error.
        */
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
        
        $strRobotsFileName = DoGetParentOrCurrentDir() . "robots.txt";
        $fileRobots = @fopen($strRobotsFileName, "r");
        $arrayLines = [];
        
        if ($fileRobots !== false)
        {
            while (!feof($fileRobots))
                $arrayLines[] = fgets($fileRobots);
            array_unshift($arrayLines, "User-agent: " . $_POST["text_keyword"]);
            fclose($fileRobots);

            $fileRobots = @fopen($strRobotsFileName, "w");
            if ($fileRobots !== false)
            {
                for ($nI = 0; $nI < count($arrayLines); $nI++)
                {
                    fputs($fileRobots, $arrayLines[$nI] . PHP_EOL);
                }
                if (fclose($fileRobots) === true)
                    PrintJavascriptLine("alert(\"'" . $_POST["text_keyword"] . "' was added to banned list in the file robots.txt...\")", 1, true);
                else
                    DoAlertLastError();   
            }
            else
                DoAlertLastError();
        }
        else
            DoAlertLastError();
    }
 
Could you maybe be missing a / to get to the robots.txt? Also, you are opening it read only, try open with "rw" maybe? edit, oops, you then did write access. X E.
 
Last edited:
I have this PHP code to write add a keyword to the end of a robots.txt file.
My web page gets an alert message saying that the keyword was successfully added to robots.txt.
However the contents of the actual robots.txt never changes.
What the hell?
I am at a loss to figure out what is happening here.
Any suggestions?

Code:
    if (isset($_POST["submit_ban_keyword"]))
    {
        /*
            file_put_contents does not work and does not throw an error.
        */
        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
       
        $strRobotsFileName = DoGetParentOrCurrentDir() . "robots.txt";
        $fileRobots = @fopen($strRobotsFileName, "r");
        $arrayLines = [];
       
        if ($fileRobots !== false)
        {
            while (!feof($fileRobots))
                $arrayLines[] = fgets($fileRobots);
            array_unshift($arrayLines, "User-agent: " . $_POST["text_keyword"]);
            fclose($fileRobots);

            $fileRobots = @fopen($strRobotsFileName, "w");
            if ($fileRobots !== false)
            {
                for ($nI = 0; $nI < count($arrayLines); $nI++)
                {
                    fputs($fileRobots, $arrayLines[$nI] . PHP_EOL);
                }
                if (fclose($fileRobots) === true)
                    PrintJavascriptLine("alert(\"'" . $_POST["text_keyword"] . "' was added to banned list in the file robots.txt...\")", 1, true);
                else
                    DoAlertLastError();  
            }
            else
                DoAlertLastError();
        }
        else
            DoAlertLastError();
    }
Is there a need for that backslash here:
Code:
...\")
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom