What are common pitfalls to avoid when specifying the storage directory for file uploads in PHP?
One common pitfall to avoid when specifying the storage directory for file uploads in PHP is using a directory that is publicly accessible. This can lead to security vulnerabilities as uploaded files can be accessed and executed by malicious users. To solve this issue, it is recommended to store uploaded files outside of the web root directory to prevent direct access.
// Specify the storage directory outside of the web root directory
$uploadDir = '/path/to/storage/directory/';
// Check if the upload directory exists, if not create it
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
// Move the uploaded file to the specified storage directory
move_uploaded_file($_FILES['file']['tmp_name'], $uploadDir . $_FILES['file']['name']);