What are the best practices for setting permissions and directory paths when using FTP functions like ftp_mkdir and ftp_copy in PHP?
When using FTP functions like ftp_mkdir and ftp_copy in PHP, it is important to set the correct permissions for newly created directories and files, as well as provide the correct directory paths for these functions to work properly. To set permissions, use the ftp_chmod function with the appropriate octal value (e.g., 0755 for read, write, and execute permissions for owner, and read and execute permissions for group and others). For directory paths, make sure to specify the full path including the FTP server's root directory.
// 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);
// Set permissions for newly created directories
$dir_path = '/path/to/new/directory';
ftp_chdir($ftp_conn, '/');
ftp_mkdir($ftp_conn, $dir_path);
ftp_chmod($ftp_conn, 0755, $dir_path);
// Copy a file to the newly created directory
$file_path = '/path/to/local/file.txt';
$remote_file = '/path/to/new/directory/file.txt';
ftp_put($ftp_conn, $remote_file, $file_path, FTP_BINARY);
// Close FTP connection
ftp_close($ftp_conn);