What are the potential pitfalls of using FTP for file uploads in PHP, and are there alternative methods?
Using FTP for file uploads in PHP can pose security risks such as exposing login credentials and potential vulnerabilities in the FTP server. An alternative method is to use PHP's built-in file handling functions like move_uploaded_file() to securely upload files to the server.
<?php
if(isset($_FILES['file'])){
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)){
echo "File is valid, and was successfully uploaded.";
} else {
echo "Upload failed";
}
}
?>