We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45950
    • 40 Posts
    I'm new to php and have successfully created a directory with a name but i am trying to figure out how to do it using the modx.user.id as the folder name.
    this is to be a hook for formit with ajaxupload where it creates a directory if it doesn't exist then on submit of the form puts the image in that directory

    mkdir("./assets/images/users/2345", 0755);

    if i try to use the[[!+modx.user.id]] it creates a folder named [[+modx.user.id]] and if you click on the folder it creates another folder endlessly.

    ultimately what I'm trying to do is check if the folder exists and if it does, do nothing else mkdir

    any help would be GREATLY appreciated!

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      If it's the current user you're doing this for, this should work:


      /* Path of containing folder - make sure it ends in a trailing slash */
      $path = MODX_ASSETS_PATH . 'myDirs/'; 
      $id = $modx->user->get('id');
      
      if (! is_dir($path . $id)) {
          mkdir($path . $id);
      }
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 45950
        • 40 Posts
        Quote from: BobRay at Jan 02, 2014, 05:37 PM
        If it's the current user you're doing this for, this should work:


        /* Path of containing folder - make sure it ends in a trailing slash */
        $path = MODX_ASSETS_PATH . 'myDirs/'; 
        $id = $modx->user->get('id');
        
        if (! is_dir($path . $id)) {
            mkdir($path . $id);
        }

        Thanks Again Bob... works in creating the folder and it uploads the image in the folder but if you go back and edit the form and try and resubmit it won't complete and the log file says:

        [2014-01-02 18:41:45] (ERROR @ /paas/c0944/www/core/cache/includes/elements/modsnippet/47.include.cache.php : 39) PHP warning: in_array() expects parameter 2 to be array, integer given

        If i remove the snippet from the hook it works as it should
        Any idea how to correct this?
          • 3749
          • 24,544 Posts
          Does the hook end with this?

          return true;


          What snippet has the ID of 47? You need to find out where in_array() is being called (it should be around lines 30-40 of that snippet) and where the second argument comes from.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 45950
            • 40 Posts
            Quote from: BobRay at Jan 03, 2014, 02:16 PM
            Does the hook end with this?

            return true;


            What snippet has the ID of 47? You need to find out where in_array() is being called (it should be around lines 30-40 of that snippet) and where the second argument comes from.
            forgot to put that...
            works great! thanks again