What is the purpose of using FTP upload in PHP scripts?
Using FTP upload in PHP scripts allows you to transfer files from your local machine to a remote server. This can be useful for automating the process of updating files on a website or transferring large files that may not be possible through traditional methods.
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$file_to_upload = "file.txt";
$remote_file_path = "/public_html/file.txt";
// Connect to FTP server
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);
// Upload file
if (ftp_put($ftp_conn, $remote_file_path, $file_to_upload, FTP_ASCII)) {
echo "File uploaded successfully";
} else {
echo "Error uploading file";
}
// Close connection
ftp_close($ftp_conn);
?>
Related Questions
- Are there any potential pitfalls in using md5() and uniqid() functions to generate GUID codes in PHP?
- How can PHP developers access nested arrays in foreach loops efficiently?
- What are some best practices for structuring and organizing scripts and styles in relation to HTML elements for optimal website performance?