How can file permissions affect the ability to export data using the PostgreSQL COPY command in PHP?

File permissions can affect the ability to export data using the PostgreSQL COPY command in PHP if the user running the PHP script does not have the necessary permissions to write to the specified file location. To solve this issue, ensure that the file path specified in the COPY command is writable by the user running the PHP script.

// Set the file path for exporting data
$file_path = '/path/to/export/data.csv';

// Ensure the file path is writable by the PHP script
if (is_writable($file_path)) {
    // Run the PostgreSQL COPY command to export data to the specified file
    $query = "COPY table_name TO '$file_path' DELIMITER ',' CSV HEADER";
    $result = pg_query($connection, $query);
    
    if ($result) {
        echo "Data exported successfully.";
    } else {
        echo "Error exporting data.";
    }
} else {
    echo "File path is not writable.";
}