How can PHP scripts be configured to save uploaded files outside of the document root for enhanced security?

When uploading files in PHP, it is important to save them outside of the document root to enhance security and prevent direct access to sensitive files. To achieve this, you can specify a directory outside of the document root where uploaded files will be saved. This way, even if an attacker manages to upload a malicious file, they won't be able to directly access it via a URL.

// Specify the directory outside of the document root where uploaded files will be saved
$uploadDirectory = '/path/to/uploaded/files/';

// Move the uploaded file to the specified directory
move_uploaded_file($_FILES['file']['tmp_name'], $uploadDirectory . $_FILES['file']['name']);