Here is the code to create a folder via a PHP program in the uploads folder where WordPress images, etc. are uploaded.


1 Get the path to the wordpress uploads folder

The PHP code to get the path to the wordpress uploads folder is shown below. It can be used with any theme or plugin you have created.

$upload_dir_data = wp_upload_dir();
$upload_dir = $upload_dir_data['basedir'];

wp_upload_dir() is a built-in function that retrieves information about the wordpress uploads folder.

When this code is executed, $upload_dir is substituted with the path of the uploads folder where WordPress images and other data are stored, such as /var/www/wp-content/uploads.

2 Create a folder of any name in the wordpress uploads folder

Next, the PHP code to create your own folder in the uploads folder is as follows

$newfolderpath = $upload_dir.'/newfoldername';
if ( wp_mkdir_p( $newfolderpath ) ) {
    // if the folder succeeds or already exists; if ( wp_mkdir_p( $newfolderpath ) )
}

wp_mkdir_p is a function built into wordpress that creates an arbitrary folder with the correct permissions. Pass $upload_dir obtained earlier and the new folder name /newfoldername to this function to have it create the folder.
(You can replace the /newfoldername part with any folder name you like.)

The final combined code is as follows

$upload_dir_data = wp_upload_dir();
$upload_dir = $upload_dir_data['basedir'];
$newfolderpath = $upload_dir.'/newfoldername';
if ( wp_mkdir_p( $newfolderpath ) ) {
    // handle if the folder succeeds or already exists.
}