We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47047
    • 43 Posts
    Hi everybody

    I have a form for updating user profile information, everything works good, but if I don't select a photo to upload when I submit the form, the photo field is overwritten with 'Array' word. I suppose the answer is here https://forums.modx.com/thread/?thread=80740 but I can't get this working. :'(

    I have this.

    Form:
    [[!UpdateProfile? &preHooks=`user_profile_image` &reloadOnSuccess=`2`    ]]
    
     <div class="msg-success"> [[+login.update_success:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]</div>
        <form class="form" action="[[~[[*id]]]]" method="post" enctype="multipart/form-data">
    	<input id="photo" name="photo" type="file"  maxlength="100000" /> ...


    user_profile_image (snippet):
    <?php
    // initialize output;
    $output = true;
    
    if($_FILES['photo']['tmp_name']!=''){
    	  // get the current user name to create the file name as  
    	$userName = $modx->user->get('username');
    	$id = $modx->user->get('id');
    
    	// valid extensions
    	$ext_array = array('jpg', 'jpeg', 'gif', 'png');
    	  
    	// create unique path for this form submission
    	$uploadpath = 'assets/uploads/'.$id.'/';
    	  
    	// 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.'.'.$ext  ;
    	   
    		  // full path to new file
    		  $myTarget = $target_path . $filename;
    			
    		  // create directory to move file iento 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 think the snippet is not validating when $_FILES is empty?

    I really appreciate your help.

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

      @yulianita
    • discuss.answer
      • 47047
      • 43 Posts
      My bad, I found the problem and solution

      1. I was using phpThumbOf extra and I just found is deprecated long time ago :$ https://github.com/modxcms/phpThumbOf I unistalled phpThumbOf and instelled pthumb, is not necessary change code of the snippet.

      2. I added a hidden input 'photobk' that will save same path if user is not updating the profile photo.
      <input id="photobk" name="photobk" type="hidden"  value="[[+photo]]" />											
      <input id="photo" name="photo" type="file"  maxlength="100000" />
      


      3. And I added assignation of this value when no one photo is selected on the user_profile_image (snippet).
       
      // if no file, don't error, but return same path saved before
        } else {
          $photo = $hook->getValue('photobk');
          $hook->setValue($sf, $photo);
        }
      


      I hope this help someone in the future wink

        @yulianita
        • 46886
        • 1,154 Posts
        Thanks for the clear solution, I am sure it will help someone