What is the difference between using copy() and move_uploaded_file() in PHP for file uploads?
The main difference between using copy() and move_uploaded_file() in PHP for file uploads is that move_uploaded_file() is specifically designed for moving an uploaded file to a new location on the server while ensuring security measures are in place, such as checking if the file was indeed uploaded via HTTP POST. On the other hand, copy() simply duplicates a file from one location to another without any regard for its source. Therefore, it is recommended to use move_uploaded_file() when handling file uploads to ensure security and prevent potential vulnerabilities.
<?php
$uploadDir = 'uploads/';
$uploadedFile = $uploadDir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadedFile)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}
?>