我在插件中使用此代码向用户配置文件添加一个额外字段,以便用户可以上载图像。它完美地上传了文件,但唯一的问题是必须删除之前上传的图像的部分不起作用。
这是我的代码:
<?php
add_action( \'show_user_profile\', \'my_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'my_show_extra_profile_fields\' );
function my_show_extra_profile_fields( $user ) {
?>
<script type="text/javascript">
var form = document.getElementById(\'your-profile\');
form.encoding = "multipart/form-data";
form.setAttribute(\'enctype\', \'multipart/form-data\');
</script>
<table class="form-table">
<tr>
<th><label for="profile_photo">Upload your profile photo</label></th>
<td>
<p>
<input type="file" name="profile_photo" id="profile_photo" />
<input type="hidden" name="action" value="save">
<input type="submit" name="submitprofilephoto" id="submitprofilephoto" class="button" value="بارگذاری">
</p>
<span class="description">
<?php
$author_profile_photo = get_author_profile_photo($user->ID);
if(is_array($author_profile_photo)):
?>
<a href="<?php echo $author_profile_photo["url"];?>" target="_blank">
<img src="<?php echo $author_profile_photo["file"];?>" height="100" width="100" />
</a>
<?php
endif;
?>
</span>
</td>
</tr>
</table>
<?php
}
function get_author_profile_photo($user_ID) {
$author_data = get_the_author_meta( \'profile_photo\', $user_ID );
$uploads = wp_upload_dir();
$author_data["file"] = $uploads["baseurl"] . $author_data["file"];
return $author_data;
}
add_action( \'personal_options_update\', \'my_save_extra_profile_fields\' );
add_action( \'edit_user_profile_update\', \'my_save_extra_profile_fields\' );
function my_save_extra_profile_fields( $user_id ) {
if ( !current_user_can( \'edit_user\', $user_id ) )
return false;
$upload=$_FILES[\'profile_photo\'];
$uploads = wp_upload_dir();
if(isset($_POST) && $_POST[\'submitprofilephoto\']!=\'\') {
if ($upload[\'tmp_name\'] && file_is_displayable_image( $upload[\'tmp_name\'] )) {
// handle the uploaded file
$overrides = array(\'test_form\' => false);
$file=wp_handle_upload($upload, $overrides);
$file["file"] = $uploads["subdir"]."/".basename($file["url"]);
// Setup the array of supported file types. In this case, it\'s just Images.
$supported_types = array( \'image/jpeg\', \'image/pjpeg\', \'image/png\' );
// Get the file type of the upload
$arr_file_type = wp_check_filetype(basename($upload[\'name\']));
$uploaded_type = $arr_file_type[\'type\'];
// Check if the type is supported. If not, throw an error.
if( $file && in_array($uploaded_type, $supported_types) ) {
if($upload[\'size\'] > 204800) {
wp_die(\'<strong>ERROR</strong>: Maximum size allowed is 200KB.\');
}
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
@unlink($author_profile_photo["file"]);
// I even tried unlink without the @ and it still doesn\'t delete the file!
update_user_meta( $user_id, \'profile_photo\', $file );
} else wp_die(\'<strong>ERROR</strong>: Allowd image formats are JPEG, JPG and PNG.\');
} elseif (!file_is_displayable_image( $upload[\'tmp_name\'] )) wp_die(\'<strong>ERROR</strong>: The file you selected is not an image!\');
}
}
?>
问题在哪里或是什么?
最合适的回答,由SO网友:aminb5 整理而成
我想出来了!问题是路径不对!感谢这篇帖子:有多少用户可以在后端删除上传的图片?
以下功能有助于:
function url_to_path_test($url){
$url=str_replace(rtrim(get_site_url(),\'/\').\'/\', ABSPATH, $url);
return $url;
}
因此,在我的代码中,如果您像这样更改“删除以前的图像”,它将起作用:
//remove previous uploaded file
$author_profile_photo = get_author_profile_photo($user_id);
$url=str_replace(rtrim(get_site_url(),\'/\').\'/\', ABSPATH, $author_profile_photo["file"]);
@unlink($url);