What could be causing the "Permission denied" error when trying to upload files via FTP in a PHP script?

The "Permission denied" error when trying to upload files via FTP in a PHP script is likely caused by insufficient permissions on the destination directory where the files are being uploaded. To solve this issue, you need to ensure that the directory has the correct permissions set to allow the PHP script to write files to it.

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_user, $ftp_pass);

// Set directory permissions
$directory = '/path/to/upload/directory';
ftp_chmod($ftp_conn, 0777, $directory);

// Upload file
$file_path = '/path/to/local/file.txt';
$remote_file = 'file.txt';
if (ftp_put($ftp_conn, $remote_file, $file_path, FTP_BINARY)) {
    echo "File uploaded successfully";
} else {
    echo "Error uploading file";
}

// Close FTP connection
ftp_close($ftp_conn);