src/FileUtils.php
public static function write2file(
        $string,
        $filename,
        $mode = self::FILE_APPEND
    ) {
    
        if (is_dir($filename)) {
            throw new \RuntimeException("Error: $filename is a directory.");
        }
        
        if ($mode) {
            if (is_writeable($filename)) {
                return file_put_contents($filename, $string, FILE_APPEND);
            } else {
                throw new \RuntimeException(
                    "Error: Cannot append to $filename. 
                     The file is either non-existing or not writeable!"
                );
            }
        }
        // Check if directory is writable
        $dir = dirname($filename);
        if (is_writeable($dir)) {
            return file_put_contents($filename, $string, LOCK_EX);
        } else {
            throw new \RuntimeException("Directory: $dir is not writeable!");
        }
    }
public static function write2file(
        $string,
        $filename,
        $mode = self::FILE_APPEND
    ) {
    
        if (is_dir($filename)) {
            throw new \RuntimeException("Error: $filename is a directory.");
        }
        
        if ($mode) {
            if (is_writeable($filename)) {
                return file_put_contents($filename, $string, FILE_APPEND);
            } else {
                throw new \RuntimeException(
                    "Error: Cannot append to $filename. 
                     The file is either non-existing or not writeable!"
                );
            }
        }
        // Check if directory is writable
        $dir = dirname($filename);
        if (is_writeable($dir)) {
            return file_put_contents($filename, $string, LOCK_EX);
        } else {
            throw new \RuntimeException("Directory: $dir is not writeable!");
        }
    }
Export string to file.
@param  string $filename
@param  string $mode
@return int  Number of bites written.

@throws \RuntimeException if file is not existing or writable.