[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Create a folder if it doesn't already exist

  • This topic is empty.
Viewing 7 posts - 16 through 22 (of 22 total)
  • Author
    Posts
  • #10810
    joaorodr84
    Participant

    If you want to avoid the file_exists vs. is_dir problem, I would suggest you to look here.

    I tried this and it only creates the directory if the directory does not exist. It does not care if there is a file with that name.

    /* Creates the directory if it does not exist */
    $path_to_directory = 'path/to/directory';
    if (!file_exists($path_to_directory) && !is_dir($path_to_directory)) {
        mkdir($path_to_directory, 0777, true);
    }
    
    #10802
    nasir-khan
    Participant

    You first need to check if directory exists file_exists('path_to_directory')

    Then use mkdir(path_to_directory) to create a directory

    mkdir( string $pathname [, int $mode = 0777 [, bool $recursive = FALSE [, resource $context ]]] ) : bool
    

    More about mkdir() here

    Full code here:

    $structure = './depth1/depth2/depth3/';
    if (!file_exists($structure)) {
        mkdir($structure);
    }
    
    #10811
    ulidu-theerake
    Participant

    Here you go.

    if (!is_dir('path/to/directory')) {
        if (!mkdir('path/to/directory', 0777, true) && !is_dir('path/to/directory')) {
            throw new \RuntimeException(sprintf('Directory "%s" was not created', 'path/to/directory'));
        }
    }
    
    #10804
    jens-trnell
    Participant

    As a complement to current solutions, a utility function.

    function createDir($path, $mode = 0777, $recursive = true) {
      if(file_exists($path)) return true;
      return mkdir($path, $mode, $recursive);
    }
    
    createDir('path/to/directory');
    

    It returns true if already exists or successfully created. Else it returns false.

    #10816
    wp-punk
    Participant

    The best way is to use the wp_mkdir_p function. This function will recursively create a folder with the correct permissions.

    Also, you can skip folder exists condition because the function returns:

    • true when the directory was created or existed before
    • false if you can’t create the directory.

    Example:

    $path = 'path/to/directory';
    if ( wp_mkdir_p( $path ) ) {
        // Directory exists or was created.
    }
    

    More: https://developer.wordpress.org/reference/functions/wp_mkdir_p/

    #10812
    usman-ahmed
    Participant

    For your specific question about WordPress, use the following code:

    if (!is_dir(ABSPATH . 'wp-content/uploads')) wp_mkdir_p(ABSPATH . 'wp-content/uploads');
    

    Function Reference: WordPress wp_mkdir_p. ABSPATH is the constant that returns WordPress working directory path.

    There is another WordPress function named wp_upload_dir(). It returns the upload directory path and creates a folder if doesn’t already exists.

    $upload_path = wp_upload_dir();
    

    The following code is for PHP in general.

    if (!is_dir('path/to/directory')) mkdir('path/to/directory', 0777, true);
    

    Function reference: PHP is_dir()

    #10805
    ashok-kumawat
    Participant

    We can create folder using mkdir. Also we can set permission for it.

    Value Permission
    0     cannot read, write or execute
    1     can only execute
    2     can only write
    3     can write and execute
    4     can only read
    5     can read and execute
    6     can read and write
    7     can read, write and execute
    
    <?PHP
      
    // Making a directory with the provision
    // of all permissions to the owner and 
    // the owner's user group
    mkdir("/documents/post/", 0770, true)
      
    ?>
    
Viewing 7 posts - 16 through 22 (of 22 total)
  • You must be logged in to reply to this topic.