WordPress.org

Forums

[resolved] mkdir function not working (5 posts)

  1. yeagerc50
    Member
    Posted 2 years ago #

    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 :)

  2. Chris
    Member
    Posted 2 years ago #

    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';
  3. yeagerc50
    Member
    Posted 2 years ago #

    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.

  4. yeagerc50
    Member
    Posted 2 years ago #

    The @ symbol suppresses the notice. @mkdir()

  5. Chris
    Member
    Posted 2 years ago #

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

    if( !file_exists( $tempDir ) && !@mkdir( $tempDir ) ) {
      $error = error_get_last();
      ...
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.