What improvements can be made to the provided PHP script for file uploading via FTP?
The provided PHP script for file uploading via FTP lacks proper error handling and security measures. To improve it, we can add checks for file size, file type, and implement error handling to provide feedback to the user in case of failures.
<?php
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
$file = $_FILES['file'];
if ($file['error'] === UPLOAD_ERR_OK) {
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
if ($conn_id && $login_result) {
$upload = ftp_put($conn_id, 'remote/path/' . $file['name'], $file['tmp_name'], FTP_BINARY);
if ($upload) {
echo "File uploaded successfully.";
} else {
echo "Failed to upload file.";
}
ftp_close($conn_id);
} else {
echo "FTP connection failed.";
}
} else {
echo "Error uploading file.";
}
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>