What steps can PHP developers take to ensure proper file permissions for writing to directories on their web server?

When writing to directories on a web server, PHP developers should ensure that the directories have the correct permissions set to allow writing. One common approach is to set the directory permissions to 755 (rwxr-xr-x) and the file permissions to 644 (rw-r--r--). This ensures that the web server can write to the directory while preventing unauthorized access.

// Set directory permissions to 755
$dir = 'path/to/directory';
chmod($dir, 0755);

// Set file permissions to 644
$file = 'path/to/file.txt';
chmod($file, 0644);