Search code examples
phpfwrite

PHP deleting the second line in a file if the file is of a certain length


When a user signs up for an account on my website their own page is created called USERNAME.php.
Whenever the username "bob" gets signed in then a new line is added to bob.php which contains the time, date and ip address of that login.

What I want to do is have a maximum of 20 lines in that file, so that the file doesn't get too big over time.
I have the first line as "Login Retrieval for bob" and then the most recent login result at the bottom. So the first line should not be deleted, but the second line should be deleted with every login (only if the amount of lines is > 20). What would be the best way to go about doing this? Thanks!


Solution

  • I assume that the .php extension is not used and that the file has up to twenty lines.

    You could explode the file in lines using the file() function, shift out the first line to save it, then use array_splice() to extract the last 19, unshift the first line back into the new array to get up to 20 entries. join them and rewrite them back to the original file.

    Better yet, write them onto a new file, then if everything went well, rename the new file onto the new.

        /**
         * @param $file    the input file
         * @param $n       total number of meaningful lines to keep (default 20)
         * @param $m       prefix lines to keep (default 1)
         *
         * @return         number of lines in case something was done
         *                 0 nothing to do
         *                 -1 file not found
         *                 -2 file not readable
         *                 -3 file not writeable
         *                 -4 write error
         *                 -8 bad parameter
         */
    
        function trim_file($file, $n = 20, $m = 1)
        {
                if (!file_exists($file))
                        return -1;
                if (!is_readable($file))
                        return -2;
                if (!is_writeable($file))
                        return -3;
                if ($m > $n)
                        return -8;
                $lines  = file($file);
                $num    = count($lines);
    
                // If file is short, no need to do anything
                if ($num <= $n)
                        return 0;
    
                $header = array_slice($lines, 0, $m);
                // Remove lines from 0 to ($num-($n-$m))
                // and replace them with the first $m lines
                array_splice($lines, 0, $num-($n-$m), $header);
    
                // Write file with new contents
                $fp = fopen($file . '.tmp', 'w');
                if (!$fp)
                        return -4;
                fwrite($fp, join('', $lines));
                fclose($fp);
    
                // Replace the file in one fell swoop.
                rename($file . '.tmp', $file);
                return count($lines);
        }