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 Answer 1

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
}
3
  • 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
    Commented Oct 22, 2015 at 19:57
  • 1
    wp_upload_dir() creates the directory automatically if it doesn't exist. No need to duplicate that work.
    – fuxia
    Commented Oct 22, 2015 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
    Commented Oct 27, 2015 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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