What are the potential pitfalls of using copy() function in PHP for file uploads?
The potential pitfall of using the copy() function for file uploads in PHP is that it does not handle file uploads properly, as it may not preserve the correct file permissions or metadata. To properly handle file uploads in PHP, you should use the move_uploaded_file() function instead.
// Example of how to properly handle file uploads in PHP using move_uploaded_file() function
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
$tmp_file = $_FILES['file']['tmp_name'];
$upload_dir = 'uploads/';
$new_file = $upload_dir . $_FILES['file']['name'];
if (move_uploaded_file($tmp_file, $new_file)) {
echo 'File uploaded successfully!';
} else {
echo 'Error uploading file.';
}
} else {
echo 'Error uploading file: ' . $_FILES['file']['error'];
}