Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I don't think I ever had practical need to create folders/files in WP before, but for a plugin I need cache (for resized images) folder in wp-content/uploads.

Which raises the question - do I really need to go through all the process with Filesystem API (including messily asking for FTP credentials when needed), or wp_mkdir_p() is good enough for this?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

wp-content/uploads/ should be writable for the server (otherwise it would be impossible to upload a file, no?). If you are going to create something under this directory, it is safe to use wp_mkdir_p().

I would only use WP_Filesystem if there is a chance the server does not have permissions to write to the location, like in wp-content/plugins/, which does not have to be writable for the server (at least I think it doesn't have to be?).

Sidenote: The File Permissions page of the Codex also talks about a wp-content/cache/ directory. Would this be a "more standard" location for cache files?

share|improve this answer
    
I am not sure but I think cache is more about stuff like page caching plugins, while my case is more about alternate versions of images... The distinction between these folders seems semantic rather than technical, no? –  Rarst May 19 '11 at 17:17

I'd try something simple, before messing around with an endless sea of possibilites/hosting setups:

$target = 'wherever';
wp_mkdir_p( $target );

if ( wp_mkdir_p( $target ) === TRUE )
{
    echo "Folder $target successfully created";
}
else
{
    new WP_Error; #... etc. Just tell where the user has to make a new folder with the name xy
    // or if you're kool, you use _doing_it_wrong(); ... ;-)
}
share|improve this answer
    
Btw: It's not tested and i got no clue whether to save the wp_mkdir_p(); can be checked after execution or if the execution of the fn should inside the if statement... (never used it before and didn't even know about the function itself until now). –  kaiser May 19 '11 at 15:04
    
Related functionality is kind of back-end-ish... I'd like to minimize interactivity. Although message in plugin's setting is worth considering. –  Rarst May 19 '11 at 17:15
    
I wouldn't think too much about it. If the users sys can't do it with default values, try to tell them what to do. As i said: Possibilites are endless and imho you're better off with just telling what the result should be to continue as to try to trouble shoot this via code or support. –  kaiser May 19 '11 at 23:35

Just a heads up on having your plugin create files/folders: my plugin did that as well, and I ran into a slew of issues with file permissions for different users. Some are windows, some are unix, some are hosted, and some home grown. It was my #1 support email once I shared the plugin.

I have refactored my code to minimize the need to touch the file systems and the upload directory is the ONLY one I use now to avoid such issues.

share|improve this answer
    
Yeah, I understand that, that's why I bother to ask question. My current overview of the issue is that if uploads is locked down enough for WP to fail then there is no sense for plugin to try and do more. And if it's good enough for WP then it's good enough for plugin. Luckily I merely need cache directory anywhere and not something more complex and prone to errors. –  Rarst Jun 24 '11 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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