MODX - 用户个人资料照片上传

时间:2012-12-12 08:27:07

标签: modx

我正在尝试允许用户将照片上传到他们的个人资料中。我确信我做错了什么......

我目前配置的内容:

更新个人资料表格

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

<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:required:email" id="email" value="[[+email]]" />
         <label for="test_field">Test Field
            <span class="error">[[+error.custom_field]]</span>
        </label>
        <input type="text" name="test_field" id="test_field" value="[[+test_field]]" /><br/>

       <div class="row clearfix">
        <div class="label">Photo<span class="error">[[+fi.error.nomination_file]]</span></div> 
        <div class="input"><input id="nomination_file" name="nomination_file:required" 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>




User_profile_image片段

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

      // 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.'.$sf.'_new', $myTarget);
        // 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;

2 个答案:

答案 0 :(得分:0)

1)修复此行中的引用

$ext_array = array(`jpg', 'jpeg', 'gif', 'png');

2)删除名称字段中的所有:required

3)而不是

    $modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);

输入

    $hook->setValue($sf, $uploadpath . $filename);

4)mkdir($target_path, 0755, true);添加

之后
  if(file_exists($myTarget) {
      chmod($myTarget,0755); //Change the file permissions if allowed
      unlink($myTarget); //remove the file
  }

答案 1 :(得分:0)

对于参考此帖子的任何人:

将user_profile_image移回预装,如下所示:

&preHooks=`user_profile_image`

并在第59行添加缺少的“)”,如下所示:

if(file_exists($myTarget)) {