How to create a folder if it does not already exist in PHP?

In my script, I’m trying to create a date-based folder in the ‘uploads’ directory if it does not already exist. The script will handle file uploads, and I want to ensure that a new folder is created every day if one does not already exist. For example, if today is January 1, 2023, I want the script to create a folder ‘uploads/2022/01/01’. Is there a way to do this in PHP? I’m using version 7.3.

Using the date function, get the current date in the YYYY/MM/DD format. It then appends the date to the uploads/ directory path to create a date-based folder. The file_exists function is used to check if the directory exists. If it does not, the mkdir function is used to create it. The third argument passed to mkdir is set to true, which tells the function to create any necessary parent directories if they do not already exist. Finally, the directory’s permissions are set to 0777 (allowing read, write, and execute permissions for all users).

$today = date('Y/m/d');
$upload_dir = 'uploads/' . $today;

if (!file_exists($upload_dir)) {
  mkdir($upload_dir, 0777, true);