2012-12-12 166 views
0

我试图让用户上传照片到他们的个人资料。我相信,我做错了什么......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; 

回答

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 
    } 
+0

嗨VASIS,
谢谢你是如此的帮助
Ankh2054

0

对于任何人谁引用了这篇文章:

移动user_profile_image回到prehooks这样的:

&preHooks=`user_profile_image` 

,并在第59行添加缺少 “)” 是这样的:

if(file_exists($myTarget)) { 
相关问题