Use this function to determine if a file is writable. A file will need to have write permissions for you to perform file_put_contents() and similar functions on it.

If the file doesn’t exist or does not have write permissions, false is returned. Can also be used on directories.

if( is_writable( 'output.txt' ) ) {
  file_get_contents( 'output.txt', 'New Content' );
}
 
$is_it = is_writable( 'does_not_exist.php' ); // false
$is_it = is_writable( '/my/existing/dir' );   // true