What is the significance of using chmod() in PHP scripts?

Using chmod() in PHP scripts is significant because it allows you to change the file permissions of a file or directory. This is important for security reasons, as you can restrict access to certain files or directories to prevent unauthorized users from viewing or modifying them. Additionally, chmod() can be used to make files executable, readable, or writable based on the needs of your script.

// Set file permissions to allow read and write access
$file = 'example.txt';
$permissions = 0644; // Read and write access for owner, read access for others
if (file_exists($file)) {
    chmod($file, $permissions);
    echo "File permissions updated successfully.";
} else {
    echo "File does not exist.";
}