We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42415
    • 115 Posts
    Using Formit I want to upload multiple (up to 6 pdf or excel) documents to a logged in USER folder, example; 'assets/upload/username'.
    The name of the folder must be the same as the username.

    I have tried to use a formit2file code I found in post 5 of this thread:

    http://forums.modx.com/thread/?thread=47603&page=1

    Here's the formit2file code:

    <?php
    // initialize output;
    $output = true;
     
    // valid extensions
    $ext_array = array('pdf', 'PDF', 'txt', 'doc', 'docx', 'rtf', 'XLS', 'XLSX', 'xls', 'xlsx',);
     
    // create unique path for this form submission
    $uploadpath = 'assets/uploads/';
     
    // you can create some logic to automatically
    // generate some type of folder structure here.
    // the path that you specify will automatically
    // be created by the script if it doesn't already
    // exist.
     
    // EXAMPLE:
    // this would put all file uploads into a new,
    // unique folder every day.
    // $uploadpath = 'assets/'uploads/'.date('Y-m-d').'/';
     
    // get full path to unique folder
    $target_path = $modx->config['base_path'] . $uploadpath;
     
    // get uploaded file names:
    $submittedfiles = array_keys($_FILES);
     
    // loop through files
    foreach ($submittedfiles as $sf) {
     
        // Get Filename and make sure its good.
        $filename = basename( $_FILES[$sf]['name'] );
     
        // Get file's extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $ext = mb_strtolower($ext); // case insensitive
     
        // is the file name empty (no file uploaded)
        if($filename != '') {
             
            // is this the right type of file?
            if(in_array($ext, $ext_array)) {
         
                // clean up file name and make unique
                $filename = mb_strtolower($filename); // to lowercase
                $filename = str_replace(' ', '_', $filename); // spaces to underscores
                $filename = date("Y-m-d_G-i-s_") . $filename; // add date & time
                 
                // full path to new file
                $myTarget = $target_path . $filename;
                 
                // create directory to move file into if it doesn't exist
                mkdir($target_path, 0755, true);
                 
                // is the file moved to the proper folder successfully?
                if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
                    // set a new placeholder with the new full path (if you need it in subsequent hooks)
                    $modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);
                    // set the permissions on the file
                    if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                     
                } else {
                    // File not uploaded
                    $errorMsg = 'There was a problem uploading the file.';
                    $hook->addError($sf, $errorMsg);
                    $output = false; // generate submission error
                }
             
            } else {
                // File type not allowed
                $errorMsg = 'Type of file not allowed.';
                $hook->addError($sf, $errorMsg);
                $output = false; // generate submission error
            }
         
        // if no file, don't error, but return blank
        } else {
            $hook->setValue($sf, '');
        }
     
    }
     
    return $output;


    Here is the attachment call (also from same forum thread):

    <div class="row clearfix">
            <div class="label">Supplementary Files <span class="error">[[+fi.error.nomination_file]]</span></div> <!-- end of .label -->
            <div class="input"><input id="nomination_file" name="nomination_file:required" type="file" value="[[+fi.nomination_file]]" maxlength="100000" /></div> <!-- end of .input -->
        </div>



    I have 2 issues I so far cannot resolve:

    1. I have repeated the attachment call 6 times (6 submit buttons) but if I select 2 or more files only the last file I submit is sent to the 'assets/upload' folder, what needs to happen is; However many attachments the user selects to be sent, if submit 6 then all 6 should be sent, if submit 5 then all 5 should be sent, at the moment if I submit 6 to upload only 1 is sent, if I submit 4 to upload only 1 is sent.....

    2. The attached file appears in the 'assets/upload' folder, I want to know how to adjust the code so it creates a new folder in the logged in users name. If the users name is harry, then, if a folder called harry does not exist then a folder should be created so... 'assets/upload/harry', if the user folder harry already exists then no need to overwrite, the user can just keep adding to their uploaded files.

    If the auto create folder for a registered user cannot be done, I can create the folder for each registered user, then the code just needs adjusting to send the attached files to the users folder every time the user submits the form.

    The way this code names the actual file does not need changing.

    I'm a bit stumped on this, if anyone can help it would be appreciated.

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

      • 4172
      • 5,888 Posts
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 42415
        • 115 Posts
        Quote from: Bruno17 at Aug 01, 2015, 05:33 AM
        you could also try http://modx.com/extras/package/ajaxupload2

        Hey thanks for the tip Bruno17.

        I will try it smiley
        • discuss.answer
          • 3749
          • 24,544 Posts
          Or plain old FileUpload.
            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
            • 42415
            • 115 Posts
            Quote from: BobRay at Aug 02, 2015, 05:26 AM
            Or plain old FileUpload.

            Hi Bob,

            Thanks for the suggestion smiley

            I just read your excellent tutorial and it ticks all the boxes of what I need to achieve (I think).

            Am I correct in assuming (from your tutorial) that I can use a TV to specify a unique file path for each logged in user?
            Each logged in user has their own unique webpage where the formit form containing the upload buttons is located so i'll give this a go to see if I can get it working.
              • 42415
              • 115 Posts
              Hi Bob,

              I tried to set this up but I am getting the following message in the upload button:

              No path to store the files was given.

              Trying to follow your tutorial I don't think the TV is set up correctly.
              In the TV the template access pages are ticked and the access permissions is ticked for the correct group.

              The part I'm unsure about is this:

              Edit those pages, and on the TV tab, set the upload path for that TV. The upload path you specify will be appended to the MODX base path. It should have a slash at the end, but not at the beginning.
              If you set the default value of the TV to something like this when you create it:
              @INHERIT some/path/


              Where do I set the upload path for the TV?
              I'm using this:
              @INHERIT assets/uploads/

              Currently I have '@INHERIT assets/uploads/' in the field 'Default Value' on 'Input Options' tab on the "UploadPath" TV
              Input Type is set to text
              Output Type is set to default
              In properties I have not set anything

              On the user page I have this inside the form:
              [[!FileUpload? &uploadTv=`UploadPath`]]

              Looking at the TV tab on the user page I can see the Name "UploadPath" and access is ticked.

              I'm pretty sure it's something simple I've missed, but I'm not sure what (this is the first TV I have ever tried to set up).
              Could you or someone explain what I've done wrong?

              edit: IModx Version Revo 2.3.3 traditional [ed. note: jimmyjazz last edited this post 8 years, 9 months ago.]
                • 3749
                • 24,544 Posts
                What is the type of the TV? It should be a plain text TV with default output.

                You can also just supply the path property in the FileUpload tag (which will be faster) like this:


                [[!FileUpload? &path=`assets/uploads/`]] 
                  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
                  • 42415
                  • 115 Posts
                  Quote from: BobRay at Aug 03, 2015, 06:18 AM
                  What is the type of the TV? It should be a plain text TV with default output.

                  You can also just supply the path property in the FileUpload tag (which will be faster) like this:


                  [[!FileUpload? &path=`assets/uploads/`]] 

                  Hi Bob,

                  Yes it was plain text with default output.

                  However, I'm experimenting with your above suggestion at the moment using:

                  [[!FileUpload? &path=`assets/uploads/transport/`]] 


                  Like this the files are saving just fine.

                  The upload button is sending the email successfully also which is cool.

                  I have one further question, currently the uploaded files are keeping their original names (which is ok), but:

                  1. Is it possible to add a date/time stamp to each file name & replace 'spaces' or 'dashes' with 'underscores'?

                  This would give a semblance of a date ordered naming convention that could be useful once a few hundred files are stored.

                  I'm also thinking (although this is a separate issue for formit2db) the form data could be saved to the database with a date/time stamp so rendering it easy to match database data with concurrently uploaded files.
                    • 3749
                    • 24,544 Posts
                    I'm afraid I don't see any way to append a date to the file name without hacking the code of the snippet.

                    I can see why the TV didn't work for you. It's a mistake in the code. It would work if the default value of the TV contained just the path (no @INHERIT), but that would kind of defeat the purpose of using a TV.
                      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
                      • 42415
                      • 115 Posts
                      Quote from: BobRay at Aug 03, 2015, 08:34 PM
                      I'm afraid I don't see any way to append a date to the file name without hacking the code of the snippet.

                      I can see why the TV didn't work for you. It's a mistake in the code. It would work if the default value of the TV contained just the path (no @INHERIT), but that would kind of defeat the purpose of using a TV.

                      Hi Bob,

                      Thanks for the info and taking the time to inform me, it is appreciated smiley

                      I'm currently experimenting with FileDownload R to display the list of word docs, pdf's.....etc that will be uploaded. It is a useful extra, it includes a date to display in the resulting table so my need to input a timestamp upon upload has diminished.

                      One thing I love about Modx is I'm always discovering new little gems that have been developed by the community. My most recent discoveries have been your FileUpload and this FileDownload R; Where I was looking for a solution these two seemed to have solved the current issue I was working on.

                      The other thing I love about the Modx community is how good folks such as yourself & Bruno are prepared to advise & furnish less experienced Modx'ers such as myself with advice, suggestions and code adjustments.

                      I gotta say, this Modx community really rocks smiley