WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] mkdir function not working

[Resolved] mkdir function not working

  • This works fine when testing it on a page with a custom template:

    add_action (‘folder’, function(){
    $tempDir = realpath(‘wp-content/themes’);
    $tempDir = $tempDir.”\used-car-lot-theme”;
    mkdir($tempDir,0700);}

    but when I move it to the register_activation_hook function in my plugin it pukes with this error:
    Warning: mkdir() [function.mkdir]: File exists
    and the folder is not created. (and no, the file does not exist).

    Help please 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Have you checked what $tempDir (e.g. with var_dump) “really” contains when calling it via the register activation hook?
    Maybe using ABSPATH instead of realpath() does a better job?

    $tempDir = ABSPATH . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'used-car-lot-theme';

    Yes, actually I did check it.
    My version produces:
    C:\xampp\htdocs\wordpress\wp-content\themes\used-car-lot-theme

    Your version produces:
    C:\xampp\htdocs\wordpress/\wp-content\themes\used-car-lot-theme

    so,
    I removed the first DIRECTORY_SEPARATOR from your line and got:
    C:\xampp\htdocs\wordpress/wp-content\themes\used-car-lot-theme

    And it worked!
    Apparently, the path TO and FROM the working directory needs to be there to create the directory.

    Now all I need to do is handle the “File Exists” notice.
    while I’m working on it ……… Any suggestions are welcome.

    Thank You Chris.

    The @ symbol suppresses the notice. @mkdir()

    Right. And you could still handle errors based on error_get_last()

    if( !file_exists( $tempDir ) && !@mkdir( $tempDir ) ) {
      $error = error_get_last();
      ...
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] mkdir function not working’ is closed to new replies.