We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42146
    • 73 Posts
    my form is the same as you posted before. everywhere there is a
    nomination_file
    i just replaced it with
    Profile_Photos
    since that is what i'm naming my extended field. oh also check path is what you want... i have mine diffrent than what you had posted above.

    so the code i posted above didwork for me, when logged in under admin profile in the front end on chrome. if i login with another user on a different browser or a chrome incocgnito window. the code no longer works..... perhaps come kind of session cache... since my photo placeholder was already populated, so it then passed the photo placehold != '' check.


    so after playing with it even more and changing the if else structure it is truly working code now smiley. has all the sanity checking error log output, so i can see where things are.

    <?php
    $modx->log(modX::LOG_LEVEL_ERROR,'Start of script-');
    // initialize output;
    $output = true;
    // get the current user name to for dicroty placement
    $userName = $modx->user->get('username');
    
    // valid extensions
    $ext_array = array('jpg', 'jpeg', 'gif', 'png');
    
    // create unique path for this form submission
    $uploadpath = 'assets/userfiles/' . $userName .'/';
    
    // get full path to unique folder
    $target_path = $modx->config['base_path'] . $uploadpath;
    
    // get uploaded file names:
    $submittedfiles = array_keys($_FILES);
    
    
    //get exsisting user profile data for Profile_Photos
    $fields = $modx->user->getOne('Profile')->get('extended');
    $Photo_fields = $fields['Profile_Photos'];
    
    $modx->log(modX::LOG_LEVEL_ERROR,'what is the user profile photo -'.$fields['Profile_Photos']);
    $modx->log(modX::LOG_LEVEL_ERROR,'linsting var is the user profile photo -'.$Photo_fields);
    
    // 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
    
      $modx->log(modX::LOG_LEVEL_ERROR,'in for loop file namnes are?-'.$filename );
    
      if($filename != '') {
        $modx->log(modX::LOG_LEVEL_ERROR,'in file name loop'.$filename);
                        // is this the right type of file?
        if(in_array($ext, $ext_array)) {
          //create file called the filename that has been sanitized
          $modx->log(modX::LOG_LEVEL_ERROR,'before sanitization'.$filename);
          $filename = strtolower(preg_replace("/[^A-Za-z0-9.]+/i", "-", $filename));
          //$filename = $filename . '.'.$ext  ;
          $modx->log(modX::LOG_LEVEL_ERROR,'after sanitization and.ext'.$filename);
          // 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
            $modx->log(modX::LOG_LEVEL_ERROR,'here was a problem uploading the file? -'.$Photo_fields);
            $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,'Type of file not allowed. -'.$Photo_fields);
          $errorMsg = 'Type of file not allowed.';
          $hook->addError($sf, $errorMsg);
          $output = false; // generate submission error
          $modx->log(modX::LOG_LEVEL_ERROR,'generate submission error');
        }
        $modx->log(modX::LOG_LEVEL_ERROR,'Completed we have a valif photo name'.$Photo_fields);
        $modx->log(modX::LOG_LEVEL_ERROR,'Completed we have a valif file name'.$filename);
      
      } // if no file, don't error, but return blank
      else {
        $modx->log(modX::LOG_LEVEL_ERROR,'filename is blank -'.$filename);
        $modx->log(modX::LOG_LEVEL_ERROR,'before the $Photo_fields != if-'.$Photo_fields);
    
        //return exsisting photofields value
        if ($Photo_fields != '')
        {
          $modx->log(modX::LOG_LEVEL_ERROR,'in the $Photo_fields != if-'.$Photo_fields);
          $modx->log(modX::LOG_LEVEL_ERROR,'Completed using already entered Photo_fields-'.$Photo_fields);      
          $hook->setValue($sf, $Photo_fields);
          
        }
        else{
          // is the file name empty (no file uploaded) and exsiting photofields empty
          $hook->setValue($sf, '');
          $modx->log(modX::LOG_LEVEL_ERROR,'at a last else hook should have a no known value for photofields -'.$Photo_fields);
          $modx->log(modX::LOG_LEVEL_ERROR,'file anem should be blank as well..... -'.$filename);
        }
      }
    }
    return $output;




    without error log output

    <?php
    // initialize output;
    $output = true;
    // get the current user name to for dicroty placement
    $userName = $modx->user->get('username');
    
    // valid extensions
    $ext_array = array('jpg', 'jpeg', 'gif', 'png');
    
    // create unique path for this form submission
    $uploadpath = 'assets/userfiles/' . $userName .'/';
    
    // get full path to unique folder
    $target_path = $modx->config['base_path'] . $uploadpath;
    
    // get uploaded file names:
    $submittedfiles = array_keys($_FILES);
    
    
    //get exsisting user profile data for Profile_Photos
    $fields = $modx->user->getOne('Profile')->get('extended');
    $Photo_fields = $fields['Profile_Photos'];
    
    // 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
    
      if($filename != '') {
        $modx->log(modX::LOG_LEVEL_ERROR,'in file name loop'.$filename);
                        // is this the right type of file?
        if(in_array($ext, $ext_array)) {
          //create file called the filename that has been sanitized
          $filename = strtolower(preg_replace("/[^A-Za-z0-9.]+/i", "-", $filename));
          //$filename = $filename . '.'.$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
        }
      } 
      else {
        //check to see if there is exsisting photofields value and return value if so
        if ($Photo_fields != '')
        {
          $hook->setValue($sf, $Photo_fields);
          
        }
        else{       
          // if no file, don't error, but return blank
          // is the file name empty (no file uploaded) and exsiting photofields empty
          $hook->setValue($sf, '');
        }
      }
    }
    return $output;

      • 41583
      • 29 Posts
      thanks Jstump you are the man ! That works perfectly now. thank so much for your efforts.
        • 42146
        • 73 Posts
        now if only i can get it to work for multiple images. which it does as is..... but still have the same issue of if a user doesn't enter a file. then the placeholder = null since the code above pulls out the profile photo field and does a != check and
        i have no idea on how to implement that with arrays / field containers...

        i guess in a sense leaving the choose file blank did in a way is like a delete for the user .... really isn't a way to clear that image out now other than replace it...

        how is the user able to remove unneeded pictures? so if site has 10k+ users that will end up using a bit of space in unused images.... [ed. note: jstump last edited this post 11 years, 4 months ago.]
          • 45526
          • 2 Posts
          You guys are awesome..! scratching my skull over this solution for days.. Thanks!