We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36448
    • 58 Posts
    Thanks a lot Jako!

    now it's works:

    $ajaxuploadFieldname = $modx->getOption('ajaxuploadFieldname', $scriptProperties, '');
    $ajaxuploadFieldformat = $modx->getOption('ajaxuploadFieldformat', $scriptProperties, 'csv');
    
    if ($ajaxuploadFieldname) {
      $attach = $hook->getValue($ajaxuploadFieldname);
      if ($ajaxuploadFieldformat == 'json') {
        $attach = json_decode($attach);
      } else {
        $attach = explode(',', $attach);
      }
    
      $hook->modx->getService('mail', 'mail.modPHPMailer');
      $count = count($attach);
        for($i=0; $i < $count; $i++)
        {
          $hook->modx->mail->mailer->AddAttachment(substr($attach[$i],1)); //remove the first slash!
          //$modx->log(modX::LOG_LEVEL_ERROR,'attach files: '.substr($attach[$i],1));
        }
    
    }
    return true;
    
      • 13428 ☆ A M B ☆
      • 1,031 Posts
      Thanks for the code.

      But why didn't you use foreach since you work all elements of the array (performance is not the answer as argued here) and why do you remove the first slash (there is none in a filename only) and don't use the full path on the server? [ed. note: Jako last edited this post 11 years, 2 months ago.]
        • 36448
        • 58 Posts
        Ooops, you are right! I changed that to foreach:
        <?php
        $ajaxuploadFieldname = $modx->getOption('ajaxuploadFieldname', $scriptProperties, '');
        $ajaxuploadFieldformat = $modx->getOption('ajaxuploadFieldformat', $scriptProperties, 'csv');
        $basepath = $modx->getOption('base_path');
        
        if ($ajaxuploadFieldname) {
          $attach = $hook->getValue($ajaxuploadFieldname);
          if ($ajaxuploadFieldformat == 'json') {
            $attach = json_decode($attach);
          } else {
            $attach = explode(',', $attach);
          }
        
          $hook->modx->getService('mail', 'mail.modPHPMailer');
        
          foreach ($attach as $value) {
            $value = substr($value,1); //remove the first slash!
            $hook->modx->mail->mailer->AddAttachment($basepath.$value);
            //$modx->log(modX::LOG_LEVEL_ERROR,'attach files: '.$value);
          }
        }
        return true;
        


        and why do you remove the first slash (there is none in a filename only) and don't use the full path on the server?

        The value in the array isn't only the filename, that included the path e.g.: "/assets/files/test.jpg"

        Do you have a better solution?

          • 13428 ☆ A M B ☆
          • 1,031 Posts
          There was a bug in AjaxUpload: If the target folder is not writable, the field contains only a filename.

          But the the file path has not to start with a slash in every case. It is based on the assets_url system setting.

          The next version of AjaxUpload will contain a new hook called AjaxUploadAttachments:
          <?php
          $ajaxuploadFieldname = $modx->getOption('ajaxuploadFieldname', $scriptProperties, '');
          $ajaxuploadFieldformat = $modx->getOption('ajaxuploadFieldformat', $scriptProperties, 'csv');
          
          $assetsPath = $modx->getOption('assets_path');
          $assetsUrl = $modx->getOption('assets_url');
          $assetsUrlLength = strlen($assetsUrl);
          
          if ($ajaxuploadFieldname) {
              $attachments = $hook->getValue($ajaxuploadFieldname);
              if ($ajaxuploadFieldformat == 'json') {
                  $attachments = json_decode($attachments);
              } else {
                  $attachments = explode(',', $attachments);
              }
          
              $hook->modx->getService('mail', 'mail.modPHPMailer');
          
              foreach ($attachments as $attachment) {
                  $attachment = substr($attachment, $assetsUrlLength);
                  $hook->modx->mail->mailer->AddAttachment($assetsPath . $attachment);
              }
          }
          return true;

          Thanks for the idea and some parts of the code.
            • 36448
            • 58 Posts
            I have to thank! AjaxUpload is a great Tool!
              • 50910
              • 3 Posts
              Hi, Jako.
              I`m try to use AjaxUpload first time and find little bug in version 1.3.0-pl.
              In snippet AjaxUpload I see that snippet return debug info every time, than it used.
              The last line is:
              return $ajaxUpload->output() . $ajaxUpload->debugOutput();

              It prints title $_SESSION["ajaxupload"] and content of $_SESSION['ajaxupload'] ['image'] array.
              I have deleted .$ajaxUpload->debugOutput() and use AjaxUpload without any problem.
              Am I right?
                • 13428 ☆ A M B ☆
                • 1,031 Posts
                Could it be that you call FormIt with &debug=`1`? This triggers the debug output of the AjaxUpload package if FormIt is called with one of the AjaxUpload hooks before the AjaxUpload snippet is working. Otherwise that information is not added (see the code).
                  • 50910
                  • 3 Posts
                  Quote from: Jako at Sep 27, 2015, 03:55 PM
                  Could it be that you call FormIt with &debug=`1`?
                  No. Don`t use any debug settings.
                  It is my FormIt call:
                  [[!FormIt?
                  &preHooks=`Formit2AjaxUpload`
                  &hooks=`AjaxUpload2Formit,email`
                  &ajaxuploadFieldname=`image`
                  &ajaxuploadTarget=`images/user/`
                  &ajaxuploadUid=`image`
                  &emailTpl=`AnketaTpl`
                  &emailSubject=`Копия Вашей анкеты`
                  &emailFrom=`[email protected]`
                  &emailTo=`[[+email]]`
                  &validate=`email:email:required,
                             fio:required,
                             tel:required,
                             vuz:required, 
                             gofrom:required,
                             formob:required,
                             gofrom:required,
                             spec:required,
                             disc:required,
                             gomonth:required,
                             isotch:required,
                             isplan:required,
                             sfera:required`
                  ]]

                  You advise me to look source code. Yes, I see, you create debug info only with debug flag set to "1".
                  But I see debug output without this flag, and understand nothing.
                  This is my AjaxUpload call in FormIt:
                  [[!AjaxUpload?
                                              &uid=`image`
                                              &allowedExtensions=`jpg,jpeg,png,gif,doc,docx,xls,xlsx,zip,arj`
                                              &thumbX=`75`
                                              &thumbY=`75`
                                              &maxFiles=`5`
                                              ]]

                  (As in your example in the documentation.)
                  Do you have any advice for me?
                    • 13428 ☆ A M B ☆
                    • 1,031 Posts
                    Could you add
                                'debug' => false,

                    in line 68 of ajaxupload.class.php?
                      • 50910
                      • 3 Posts
                      Yes, I do it.
                      We get this block of code in result.
                      $this->config = array_merge($this->config, array(
                                  'debug' => false,
                                  'uid' => $this->modx->getOption('uid', $config, md5($this->modx->getOption('site_url') . '-' . $resourceId), true),
                                  'uploadAction' => $assetsUrl . 'connector.php',
                                  'newFilePermissions' => '0664',
                                  'filecopierPath' => '', // not implemented yet
                              ));


                      I added your line of code in line 68, saved, checked, cleaned cache of MODX and browser. Then run my form.
                      But nothihg changed(. You can see it yourself: http://uchet.nuzenavtor.ru/index.php?id=12

                      P.S. I`d found some troubles with my hoster and desided to stop this experiment until I select new hosting. [ed. note: vitaliz last edited this post 11 years ago.]