How can a target directory be specified within a PHP script for FTP file uploads?

To specify a target directory for FTP file uploads in a PHP script, you can use the `ftp_chdir()` function to change the current directory on the FTP server before uploading the file. This function allows you to navigate to the desired directory where you want to upload the file.

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

// Change to target directory
$target_dir = '/uploads';
ftp_chdir($ftp_conn, $target_dir);

// Upload file to target directory
$file_path = 'path/to/local/file.txt';
$file_name = 'file.txt';
ftp_put($ftp_conn, $file_name, $file_path, FTP_BINARY);

// Close FTP connection
ftp_close($ftp_conn);