How can one specify the destination directory for uploaded files in a PHP FTP script?
When uploading files using PHP FTP script, you can specify the destination directory by including the directory path in the remote file name. This can be done by concatenating the directory path with the file name before uploading the file using the ftp_put() function.
// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_user, $ftp_pass);
// Specify destination directory
$destination_dir = '/path/to/destination/directory/';
// Local file to upload
$local_file = 'file.txt';
// Remote file name including destination directory
$remote_file = $destination_dir . basename($local_file);
// Upload file to FTP server
if (ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY)) {
echo "File uploaded successfully to $remote_file";
} else {
echo "Failed to upload file";
}
// Close FTP connection
ftp_close($conn_id);
Keywords
Related Questions
- What are the advantages and disadvantages of using PEAR::HTTP_Request for file uploads in PHP?
- How does the use of mb_internal_encoding() impact the output when reading files from a ZipArchive in PHP?
- How can the error_reporting function be utilized to troubleshoot PHP scripts that are encountering issues with database queries?