In PHP, what methods or functions can be used to move a file from one directory to another, especially when dealing with user-uploaded content?
When dealing with user-uploaded content in PHP, you may need to move files from one directory to another. This can be done using the `move_uploaded_file()` function, which moves an uploaded file to a new location. It is important to validate and sanitize user input to prevent security vulnerabilities.
$uploadedFile = $_FILES['file']['tmp_name'];
$targetDirectory = 'uploads/';
$targetFile = $targetDirectory . basename($_FILES['file']['name']);
if (move_uploaded_file($uploadedFile, $targetFile)) {
echo "File moved successfully.";
} else {
echo "Error moving file.";
}
Related Questions
- What are some best practices for optimizing caching in PHP to improve performance?
- What are the potential pitfalls of relying on client-side caching mechanisms when developing PHP applications?
- What potential security risks are present in the provided PHP login script, and how can they be mitigated?