我有一个前端用户配置文件模板,我正在工作,一切都很好,除了上传配置文件图片。我创建了一个自定义用户字段,我正在尝试使用按钮文件类型上载图片并将URL存储在自定义字段中。
看一看-https://pastebin.com/rPErwCgU
我目前已经在使用此代码进行前端帖子上传,它允许多张图片。我需要找出如何为只有1张照片的用户执行此操作,并将其存储在附加到当前用户的自定义字段中--
if (!empty($_FILES[\'sightMulti\'][\'tmp_name\'][0])) {
$i = 1;
$files = $_FILES[\'sightMulti\'];
foreach ($files[\'name\'] as $key => $value) {
if ($files[\'name\'][$key]) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$_FILES = array("sight" . $i => $file);
$newuploadMulti = sight("sight" . $i, $pid);
if ($i == 1) {
update_post_meta($pid, \'_thumbnail_id\', $newuploadMulti);
}
add_post_meta($pid, \'imic_property_sights\', $newuploadMulti, false);
}
$i++;
}
}
表单中的字段--
<div class="col-md-12 col-sm-12">
<label><?php _e(\'Upload Images\', \'framework\'); ?></label>
<p><?php _e(\'Upload images that are best clicked for better appearance of your property\', \'framework\'); ?></p>
</div>
<div class="row" id="multiplePhotos" style="margin-top:15px;">
<div class="col-md-12 col-sm-12">
<?php
echo\'<div class="image-placeholder" id="photoList">\';
if (!empty($property_sights_value)) {
foreach ($property_sights_value as $property_sights) {
$default_featured_image = get_post_meta($Property_Id, \'_thumbnail_id\', true);
if ($default_featured_image == $property_sights) {
$def_class = \'default-feat-image\';
} else {
$def_class = \'\';
}
echo \'<div class="col-md-2 col-sm-2">\';
echo \'<div id="property-img"><div id="property-thumb" class="\' . $def_class . \'"><a id="feat-image" class="accent-color default-image" data-original-title="Set as default image" data-toggle="tooltip" style="text-decoration:none;" href="#"><div class="property-details" style="display:none;"><span class="property-id">\' . $Property_Id . \'</span><span class="thumb-id">\' . $property_sights . \'</span></div><img src="\' . wp_get_attachment_thumb_url($property_sights) . \'" class="image-placeholder" id="filePhoto2" alt=""/></a>\';
if (get_query_var(\'site\')) {
echo \'<input rel="\' . $Property_Id . \'" type="button" id="\' . $property_sights . \'" value="Remove" class="btn btn-sm btn-default remove-image">\';
}
echo \'</div></div>\';
echo \'</div>\';
}
}
echo \'</div>\';
?>
<input id="filePhotoMulti" type="file" name="sightMulti[]" multiple onChange="previewMultiPhotos();">
</div>
</div>
UPDATE
这是我的右上方的字段,其他字段可以保存罚款-
if ( !empty( $_POST[\'agent-image\'] ) )
update_user_meta( $current_user->ID, \'agent_image\', esc_attr( $_POST[\'agent-image\'] ) );
if(!empty($_FILES)) {
$uploaddir = wp_upload_dir();
$file = $_FILES[agent-image];
$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );
move_uploaded_file( $file[\'tmp_name\'] , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
}
我的输入字段--
<input type="file" name="agent-image">
我是如何在作者身上使用它的。php模板--
<?php
$author_pic = get_the_author_meta(\'agent-image\', $user->ID);
echo \'<div><img src="\'. $author_pic .\'" alt="\'. $userName .\'" class="img-thumbnail authppic"></div>\'; ?>
UPDATE 2
代码现在导致该页面出现HTTP 500错误--
if ( !empty( $_FILES[agent-image][name] )) {
$uploaddir = wp_upload_dir();
$file = $_FILES[agent-image];
$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file[\'name\'] );
move_uploaded_file( $file[\'tmp_name\'] , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
update_user_meta( $current_user->ID, \'agent_image\', $attach_id ));
}