What are the best practices for handling file uploads in PHP to avoid permission issues?

When handling file uploads in PHP, it's important to ensure that the directory where the files will be stored has the correct permissions set to allow the web server to write to it. To avoid permission issues, you can create a dedicated directory for file uploads and set its permissions to allow the web server to write to it.

// Set the directory where uploaded files will be stored
$uploadDir = 'uploads/';

// Create the directory if it doesn't exist
if (!file_exists($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

// Set the correct permissions for the directory
chmod($uploadDir, 0777);