We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 52700
    • 15 Posts
    Hiiii,
    When i gave code to save form and submit the error comes as
    Parse error: syntax error, unexpected '$ext_array' (T_VARIABLE) in /public_html/sitename/core/cache/includes/elements/modsnippet/39.include.cache.php on line 6

    The code I used is

    <?php
    // initialize output;
    $output = true;
      
    // valid extensions
    $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
      
    // create unique path for this form submission
    $uploadpath = 'assets/form-uploads/';
      
    // Unique file creation 
     function createUploadFilename($currentPath,$currentFilename) {
           
          // Create unique filename
          if(file_exists($currentPath . $currentFilename)) {
    
                  $i = 0;
                   
                  do {
                    $nF =  $i . $currentFilename;
                    $i++;
                
                  } while (file_exists($currentPath . $nF));
          }
           
          if(isset($nF)) {
              $newFilename = $currentPath . $nF;
               
          } else {
               
              $newFilename = $currentPath . $currentFilename;
          }
          
         return $newFilename;
     }
      
    // get full path to unique folder
    $target_path = $modx->config['base_path'] . $uploadpath;
    $modx->log(modX::LOG_LEVEL_ERROR, 'Target path: ' . $target_path );
     
    // get uploaded file names:
    $submittedfiles = array_keys($_FILES);
      
    // loop through files
    $countFiles = 1;
    foreach ($submittedfiles as $sf) {
      
        // Get Filename and make sure its good.
        $filename = basename( $_FILES[$sf]['name'] );
        $modx->log(modX::LOG_LEVEL_ERROR, 'Filename: ' . $filename);
         
        // Get file's extension
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $ext = mb_strtolower($ext); // case insensitive
        $modx->log(modX::LOG_LEVEL_ERROR, 'Extension: ' . $ext );
      
        // 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 filename
                $filename = $modx->resource->cleanAlias($filename);
                $modx->log(modX::LOG_LEVEL_ERROR, 'Cleaned file: ' . $filename );
                 
                $filename = date("Y-d-m_") . $filename; // add date
                 
                // create directory to move file into if it doesn't exist
               if(!file_exists($target_path)) {
                   mkdir($target_path, 0755, true);
                    
               }
                
                // Create unique filename
                $myTarget = createUploadFilename($target_path,$filename);
                $modx->log(modX::LOG_LEVEL_ERROR, 'Clean and unique filename: ' . $myTarget );
     
                // 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.bestand' . $countFiles,  $modx->getOption('site_url') . $uploadpath . $filename);
                     
                    $modx->log(modX::LOG_LEVEL_ERROR, 'Placeholder set: ' . 'fi.bestand' . $countFiles);
                    // set the permissions on the file
                    if (!chmod($myTarget, 0644)) { /*some debug function*/ }
                      
                } else {
                    // File not uploaded
                     $modx->log(modX::LOG_LEVEL_ERROR, 'Bestand kon niet worden geupload!' );
                    $errorMsg = 'There was a problem uploading the file.';
                    $hook->addError($sf, $errorMsg);
                    $output = false; // generate submission error
                }
              
            } else {
                // File type not allowed
                 $modx->log(modX::LOG_LEVEL_ERROR, 'Bestandtype niet toegestaan!');
                $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, '');
        }
    $countFiles++; 
    }
    $modx->log(modX::LOG_LEVEL_ERROR, 'Continue? ' . $output);
     
    $hook->setValue($sf, '');
    return $output;
    return;
    



    Please help anyone.....
    Thnx in advance
      • 30585
      • 833 Posts
      You had a character encoding problem. Here' your code reformatted. It should work now:
      <?php
      // initialize output;
      $output = true;
      
      // valid extensions
      $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
      
      // create unique path for this form submission
      $uploadpath = 'assets/form-uploads/';
      
      // Unique file creation
      function createUploadFilename($currentPath, $currentFilename)
      {
          
          // Create unique filename
          if (file_exists($currentPath . $currentFilename)) {
              $i = 0;
              do {
                  $nF = $i . $currentFilename;
                  $i++;
              } while (file_exists($currentPath . $nF));
          }
          
          if (isset($nF)) {
              $newFilename = $currentPath . $nF;
          } else {
              
              $newFilename = $currentPath . $currentFilename;
          }
          return $newFilename;
      }
      // get full path to unique folder
      $target_path = $modx->config['base_path'] . $uploadpath;
      $modx->log(modX::LOG_LEVEL_ERROR, 'Target path: ' . $target_path);
      
      // get uploaded file names:
      $submittedfiles = array_keys($_FILES);
      
      // loop through files
      $countFiles = 1;
      foreach ($submittedfiles as $sf) {
          
          // Get Filename and make sure its good.
          $filename = basename($_FILES[$sf]['name']);
          $modx->log(modX::LOG_LEVEL_ERROR, 'Filename: ' . $filename);
          
          // Get file's extension
          $ext = pathinfo($filename, PATHINFO_EXTENSION);
          $ext = mb_strtolower($ext); // case insensitive
          $modx->log(modX::LOG_LEVEL_ERROR, 'Extension: ' . $ext);
          
          // 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 filename
                  $filename = $modx->resource->cleanAlias($filename);
                  $modx->log(modX::LOG_LEVEL_ERROR, 'Cleaned file: ' . $filename);
                  
                  $filename = date("Y-d-m_") . $filename; // add date
                  
                  // create directory to move file into if it doesn't exist
                  if (!file_exists($target_path)) {
                      mkdir($target_path, 0755, true);
                      
                  }
                  
                  // Create unique filename
                  $myTarget = createUploadFilename($target_path, $filename);
                  $modx->log(modX::LOG_LEVEL_ERROR, 'Clean and unique filename: ' . $myTarget);
                  
                  // 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.bestand' . $countFiles, $modx->getOption('site_url') . $uploadpath . $filename);
                      
                      $modx->log(modX::LOG_LEVEL_ERROR, 'Placeholder set: ' . 'fi.bestand' . $countFiles);
                      // set the permissions on the file
                      if (!chmod($myTarget, 0644)) {
                          /*some debug function*/
                      }
                      
                  } else {
                      // File not uploaded
                      $modx->log(modX::LOG_LEVEL_ERROR, 'Bestand kon niet worden geupload!');
                      $errorMsg = 'There was a problem uploading the file.';
                      $hook->addError($sf, $errorMsg);
                      $output = false; // generate submission error
                  }
              } else {
                  // File type not allowed
                  $modx->log(modX::LOG_LEVEL_ERROR, 'Bestandtype niet toegestaan!');
                  $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, '');
          }
          $countFiles++;
      }
      $modx->log(modX::LOG_LEVEL_ERROR, 'Continue? ' . $output);
      
      $hook->setValue($sf, '');
      return $output;
      return;
        A MODx Fanatic
        • 52700
        • 15 Posts
        Quote from: treigh at Oct 03, 2016, 08:41 AM
        You had a character encoding problem. Here' your code reformatted. It should work now:
        <!--?php
        // initialize output;
        $output = true;
        
        // valid extensions
        $ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf');
        
        // create unique path for this form submission
        $uploadpath = 'assets/form-uploads/';
        
        // Unique file creation
        function createUploadFilename($currentPath, $currentFilename)
        {
            
            // Create unique filename
            if (file_exists($currentPath . $currentFilename)) {
                $i = 0;
                do {
                    $nF = $i . $currentFilename;
                    $i++;
                } while (file_exists($currentPath . $nF));
            }
            
            if (isset($nF)) {
                $newFilename = $currentPath . $nF;
            } else {
                
                $newFilename = $currentPath . $currentFilename;
            }
            return $newFilename;
        }
        // get full path to unique folder
        $target_path = $modx--->config['base_path'] . $uploadpath;
        $modx->log(modX::LOG_LEVEL_ERROR, 'Target path: ' . $target_path);
        
        // get uploaded file names:
        $submittedfiles = array_keys($_FILES);
        
        // loop through files
        $countFiles = 1;
        foreach ($submittedfiles as $sf) {
            
            // Get Filename and make sure its good.
            $filename = basename($_FILES[$sf]['name']);
            $modx->log(modX::LOG_LEVEL_ERROR, 'Filename: ' . $filename);
            
            // Get file's extension
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            $ext = mb_strtolower($ext); // case insensitive
            $modx->log(modX::LOG_LEVEL_ERROR, 'Extension: ' . $ext);
            
            // 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 filename
                    $filename = $modx->resource->cleanAlias($filename);
                    $modx->log(modX::LOG_LEVEL_ERROR, 'Cleaned file: ' . $filename);
                    
                    $filename = date("Y-d-m_") . $filename; // add date
                    
                    // create directory to move file into if it doesn't exist
                    if (!file_exists($target_path)) {
                        mkdir($target_path, 0755, true);
                        
                    }
                    
                    // Create unique filename
                    $myTarget = createUploadFilename($target_path, $filename);
                    $modx->log(modX::LOG_LEVEL_ERROR, 'Clean and unique filename: ' . $myTarget);
                    
                    // 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.bestand' . $countFiles, $modx->getOption('site_url') . $uploadpath . $filename);
                        
                        $modx->log(modX::LOG_LEVEL_ERROR, 'Placeholder set: ' . 'fi.bestand' . $countFiles);
                        // set the permissions on the file
                        if (!chmod($myTarget, 0644)) {
                            /*some debug function*/
                        }
                        
                    } else {
                        // File not uploaded
                        $modx->log(modX::LOG_LEVEL_ERROR, 'Bestand kon niet worden geupload!');
                        $errorMsg = 'There was a problem uploading the file.';
                        $hook->addError($sf, $errorMsg);
                        $output = false; // generate submission error
                    }
                } else {
                    // File type not allowed
                    $modx->log(modX::LOG_LEVEL_ERROR, 'Bestandtype niet toegestaan!');
                    $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, '');
            }
            $countFiles++;
        }
        $modx->log(modX::LOG_LEVEL_ERROR, 'Continue? ' . $output);
        
        $hook->setValue($sf, '');
        return $output;
        return;
        Thnx for your response But the code shows error again....