What functions in PHP can be used to handle file uploads securely and efficiently?
To handle file uploads securely and efficiently in PHP, the functions `move_uploaded_file()` and `is_uploaded_file()` can be used. These functions help ensure that uploaded files are moved to a secure location on the server and that only files uploaded through the HTTP POST method are processed.
<?php
$uploadDirectory = 'uploads/';
if(isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])){
$fileName = $_FILES['file']['name'];
$destination = $uploadDirectory . $fileName;
if(move_uploaded_file($_FILES['file']['tmp_name'], $destination)){
echo 'File uploaded successfully.';
} else {
echo 'Error uploading file.';
}
} else {
echo 'Invalid file upload.';
}
?>
Keywords
Related Questions
- What are the best practices for securely handling user input in PHP?
- What are the common pitfalls when handling file uploads in PHP, as seen in the provided code snippet?
- What are potential security risks associated with storing sensitive information like passwords in cookies in PHP web applications?