0

我将以下页面片段和表单配置为允许用户将照片上传到他们的个人资料。当用户更新他的个人资料但未指定图像时,上传的原始图像将从个人资料扩展字段中删除。它回到空白。

问题:有没有办法防止这种情况发生。希望允许用户更新个人资料,但不删除之前上传的图像 URL。

片段

<?php
// 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;



资源

[[!UpdateProfile? &useExtended=`1`  &postHooks=`redirect_profile_update` &preHooks=`user_profile_image`]]

<div class="update-profile">
    <div class="updprof-error">[[+error.message]]</div>
    [[+login.update_success:if=`[[+login.update_success]]`:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]

    <form class="form" enctype="multipart/form-data" action="[[~[[*id]]]]" method="post">
        <input type="hidden" name="nospam:blank" value="" />

        <label for="fullname"><i class="icon-user"></i> <strong>[[!%login.fullname? &namespace=`login` &topic=`updateprofile`]]</strong>
            <span class="error">[[+error.fullname]]</span>
        </label>
        <input type="text" name="fullname" id="fullname" value="[[+fullname]]" />

        <label for="email"><i class="icon-envelope"></i> <strong>[[!%login.email]]</strong>
            <span class="error">[[+error.email]]</span>
        </label>
        <input type="text" name="email" id="email" value="[[+email]]" />


       <div class="row clearfix">
        <div class="label"><img src="[[+nomination_file:phpthumbof=`w=120&h=120&zc=1&fltr[]=ric|20|20`
]]" /></div> <span class="error">[[+fi.error.nomination_file]]</span>
        <div class="input"><input id="nomination_file" name="nomination_file" type="file" value="[[+fi.nomination_file]]" maxlength="100000" /></div>
    </div>

        <br class="clear" />

       <button class="btn-info btn btn-large" type="submit" name="login-updprof-btn">Update Profile</button>
    </form>
</div>
4

2 回答 2

0

这是一种有效的方法

PHP 片段

<?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;



更改个人资料表格

[[!UpdateProfile? &useExtended=`1`  &postHooks=`redirect_profile_update` &preHooks=`user_profile_image`]]

<div class="update-profile">
    <div class="updprof-error">[[+error.message]]</div>
    [[+login.update_success:if=`[[+login.update_success]]`:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]

    <form class="form" enctype="multipart/form-data" action="[[~[[*id]]]]" method="post">
        <input type="hidden" name="nospam:blank" value="" />

        <label for="fullname"><i class="icon-user"></i> <strong>[[!%login.fullname? &namespace=`login` &topic=`updateprofile`]]</strong>
            <span class="error">[[+error.fullname]]</span>
        </label>
        <input type="text" name="fullname" id="fullname" value="[[+fullname]]" />

        <label for="email"><i class="icon-envelope"></i> <strong>[[!%login.email]]</strong>
            <span class="error">[[+error.email]]</span>
        </label>
        <input type="text" name="email" id="email" value="[[+email]]" />


       <div class="row clearfix">
        <div class="label"><img src="[[+Profile_Photos:phpthumbof=`w=120&h=120&zc=1&fltr[]=ric|20|20`
]]" /></div> <span class="error">[[+fi.error.Profile_Photos]]</span>
        <div class="input"><input id="Profile_Photos" name="Profile_Photos" type="file" value="[[+fi.Profile_Photos]]" maxlength="100000" /></div>
    </div>

        <br class="clear" />

       <button class="btn-info btn btn-large" type="submit" name="login-updprof-btn">Update Profile</button>
    </form>
</div>
于 2012-12-19T07:24:03.700 回答
0

不记得确切的 formIt 是如何工作的,但请尝试从此处的占位符中删除fi.前缀:

<div class="input"><input id="nomination_file" name="nomination_file" type="file" value="[[+nomination_file]]" maxlength="100000" /></div>

这样,输入应该填充现有的图像路径,如果没有上传另一个图像,则将重新提交。

于 2012-12-16T13:46:22.683 回答