We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19153
    • 53 Posts
    I have this working now it was not the snippet but the form.

    I think it was a problem with me requiring the file to be uploaded in the form . Rather than using nomination_file:required in the form code it seems to only work for me in the snippet code:

    &validate=`nomination_file:required`


    Im extremely happy now to have a test website running that creates a resource and uploads a photo into my assets directory.

    The only thing remaining for me to do now is to find out how to get the uploaded image to show in the new resource. I was wondering if there was a way of having the new image path placed inside an image TV?

    Would this be possible or am I going about this the wrong way?
      • 23050
      • 1,842 Posts
      Great smiley

      About your question, I don't know :/ As I couldn't get working it...
        • 16430
        • 217 Posts
        Got problem with the latest snippet code, it sucessfully upload image but input this in the TV:
        (filename).jpg||image/jpeg||/tmp/php5/phpFi8L01||0||657369


        instead of full path.
        How can I change filename to resource id for which is file uploaded? ([*id]-some_extension.jpg)
          • 16430
          • 217 Posts
          Seems like messyp has the same problem, this snippet is probably not made to input any file path to TV...
            • 16430
            • 217 Posts
            Ok, I manage to combine formit2file and formit2resource into one snippet.
            It create new resource, upload file, rename file to resource_id-input_name.ext, and fill TV with uploaded file URL.


            In FormIt call, just delete hook name formit2file and formit2resource and replace it with new one.
            I am not good at php so if anyone can find any bug or optimize the code, would be great.

            Does anybody know how to redirect to newly created page after form submit or send page id by email?


            P.S. I cannot paste the code so it is in textfile... [ed. note: kudykam last edited this post 11 years, 1 month ago.]
              • 24900
              • 44 Posts
              Quote from: messyp at Apr 11, 2012, 08:51 AM
              Thanks for this but stil no joy!

              your missing
              enctype='multipart/form-data'
              in your <form>
                • 24900
                • 44 Posts
                Quote from: landonpoburan at Dec 21, 2010, 06:17 PM
                I created a hook called formit2file

                Freakin' brilliant dude! Cheers
                  • 35756
                  • 166 Posts
                  Hello there!

                  This was something I needed for my current project too, thanks for sharing!

                  But one issue I have: I use this inside an UpdateProfile call. There are several input-fields the user can enter some content in, additionally there's one input-field type="file" for the image-upload. There is a placeholder to display the image too.

                  When the user has uploaded a file this image gets displayed after submitting and validation success. But when the user edits another input-fields and does not change the file-input, the varible that got saved before gets deleted.

                  I'm using this script:

                  // initialize output;
                  $output = true;
                    // get the current user name to create the file name as  
                  $userName = $modx->user->get('username');
                  
                  // valid extensions
                  $ext_array = array('jpg', 'jpeg', 'gif', 'png');
                  
                  // 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)) {
                  
                        //create file called the user name + pic
                        $filename = $userName . "-pic".'.'.$ext  ;
                  
                        // 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);
                    if(file_exists($myTarget)) {
                        chmod($myTarget,0755); //Change the file permissions if allowed
                        unlink($myTarget); //remove the file
                    }
                  
                        // 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)
                          $hook->setValue($sf, $uploadpath . $filename);
                          // 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;


                  I guess this part on line 81 is responsible for overwriting the previous value that got stored before:

                  $hook->setValue($sf, '');


                  So I tried to comment this line out by using this:

                  //$hook->setValue($sf, '');


                  but without success.

                  SO how can I keep the value if there is no new file added but the form by using the UpdateProfile-call gets submitted?
                    • 1081
                    • 7 Posts
                    Quote from: Bruno17 at Feb 29, 2012, 07:17 PM
                    try

                    $hook->setValue('target',$myTarget);
                    Hello Bruno,
                    You help me the other day with a getResource issue.
                    Can you help me with this one:
                    I'm uploding files with formit2file.
                    This snippet sets uploading path: &path = `assets/uploads/`
                    But how can I change the path according to the option shosen by user in a select field?
                    I tried this &path = `assets/images/[[+fi.language]]/[[+fi.country]]/[[+fi.location]]/` but something tells me this is stupid.
                    Can you help?
                    Thank you
                    Alex
                      • 8168
                      • 1,118 Posts
                      Hi, I am using this form script - but want to return the file path / file names of the uploaded files to the input fields they are uploaded from... how can I do this? I have a form with 2 file input field, called fileinput1 and fileinput2 - how can I add in some code to get the file path and file name of the files uploaded on each as a session var?