0

My plugin uses the uploads folder to store some files for configuration (as that folder is writable on all installs and I've had issues with Wordpress deleting files elsewhere on some installations).

However, when WordPress is first created, there's no uploads folder. If you manually go to the media section and upload something, it will create it, but I want to be able to do this programmatically. Is there a way?

1

This should work - using wp_upload_dir to get the path, then checking to see if it exists - and creating it if it doesn't.

$upload_dir = wp_upload_dir();
if (!is_dir($upload_dir['path'])) {  // directory doesn't exist
    mkdir($structure, 0744, true); // the mode to use here may vary based on your hosting environment
}
  • Thanks, and creating the folder myself won't cause issues? (I have had issues in some installations where WordPress deletes files I create) – Don Rhummy Oct 22 '15 at 19:57
  • 1
    wp_upload_dir() creates the directory automatically if it doesn't exist. No need to duplicate that work. – fuxia Oct 22 '15 at 21:05
  • @toscho You're correct! The function contains if ( ! wp_mkdir_p( $uploads['path'] ) ) Can you please post that as an answer? – Don Rhummy Oct 27 '15 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.