How can the "Unable to access" error be resolved when using FTP to upload files in PHP?

The "Unable to access" error when using FTP to upload files in PHP can be resolved by ensuring that the correct permissions are set on the target directory. You can use the PHP function `ftp_chmod` to set the permissions on the directory before attempting to upload the file.

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");

// Login to FTP server
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// Set permissions on target directory
$directory = '/path/to/target/directory';
$permissions = 0777; // Change permissions as needed
if (ftp_chmod($ftp_conn, $permissions, $directory) !== false) {
    echo "Permissions set successfully on $directory\n";
} else {
    echo "Failed to set permissions on $directory\n";
}

// Upload file to FTP server
$file = 'file.txt';
$remote_file = '/path/to/remote/file.txt';
if (ftp_put($ftp_conn, $remote_file, $file, FTP_BINARY)) {
    echo "File uploaded successfully\n";
} else {
    echo "Failed to upload file\n";
}

// Close FTP connection
ftp_close($ftp_conn);