PHP – Function to read and write a TXT file

I’m making a function on WordPress to get the content of the robots.txt file. If the file doesn’t exist, create it with default content. I will use it for my options page. Well, this is my code, it should work almost creating the file, but it doesn’t:

function get_robots($robots_file) {

$robots_file = get_home_path() . 'robots.txt'; //The robots file.
$dir = get_home_path(); //The root directory

if(is_file($robots_file)){
    $handle = fopen($robots_file, "r");
    $robots_content = fread($handle, filesize($robots_file));        
    fclose($handle);

} else {
    $default_content = "User-agent: *nDisallow:";
    chmod($dir, 0777);
    $handle = fopen($robots_file, "w+");
    $robots_content = fwrite($handle, $default_content);        
    fclose($handle);

}

chmod($dir, 0744);
return $robots_content;

}

I’m not sure if the problem is is_file, or the fopen($robots_file, "w+" (should it be “r”?) after the else. And I’m not sure about the permissions. Is the 777 needed? Is the 744 the default for the root directory of WordPress?

Read More

And I use the return to use it as variable later; I suppose the fopen is already creating the file. Am I right?

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. The first thing, I would use completely different functions, you have file_put_contents() and file_get_contents() for such simple operations.

    So possible simpler solution is:

    function get_robots() {
    
    $robots_file = get_home_path() . 'robots.txt'; //The robots file.
    
    
    if(file_exists($robots_file)){
        return file_get_contents($robots_file);
    
    } else {
        $default_content = "User-agent: *nDisallow:";
        file_put_contents($robots_file, $default_content);
        return $default_content;
    }
    
    }
    

    I don’t see any point to pass $robots_file as function argument so I removed it. You should check if this code simple works.

    I also don’t see any reason to change $dir permissions as you showed in your code. It should be rather set manually and you definitely shouldn’t change your root directory permission in such function.

    EDIT

    Because this function uses get_home_path() and this one is available probably only on admin panel you have to do it in different way. You may add the following code to the end of your index.php file:

    function get_robots($path)
    {
        $robots_file = $path . DIRECTORY_SEPARATOR . 'robots.txt'; //The robots file.
        if(file_exists($robots_file)){
            return file_get_contents($robots_file);
    
        } else {
            $default_content = "User-agent: *nDisallow:";
            file_put_contents($robots_file, $default_content);
            return $default_content;
        }
    }
    
    get_robots(getcwd());
    

    (Of course if you want, you may move get_robots() function to some other files.

    However you should consider if this is the best approach. You will run this function each time your site will be viewed and it’s tiny waste (in fact you will probably want to create robots.txt file just once). You could for example create robots.php file and if you want to run it you can run http://yourwordpressurl/robots.php. It’s of course your call.