我正在向db提交图像,如下所示:
if (isset($_POST[\'uploadImgCustom\'])) {
$myNewImg = get_post_meta($id, \'usp-file-single\', true);
}
$attachments = get_posts(array(
\'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'post_status\' =>\'any\',
\'post_parent\' => $post->ID
));
if ($attachments) {
foreach ( $attachments as $attachment ) {
$myNewImg = wp_get_attachment_url( $attachment->ID );
update_post_meta( $id, \'usp-file-single\', $myNewImg);
}
?>
<img src="<?php echo $myNewImg; ?>" class="img-responsive">
<?php }
然后我会:
if($_SERVER[\'REQUEST_METHOD\']=="POST") {
if (\'AGGIORNA ALLEGATO\' === ($_POST[\'uploadImgCustom\'])) {
if ($_FILES[\'postImage\']) {
$attachments = get_attached_media( \'\', $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, \'true\' );
}
foreach ($_FILES as $file => $array) {
if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
//Add your error action
} else {
$attach_id = media_handle_upload( $file, $id );
}
}
}
}
}
新文件已正确上载,页面在上载完成后会自动刷新。然而,我在页面上看到了旧图像,但是如果我刷新页面,我会看到新文件。这就像保留缓存,而不是一开始就读取新数据。
下面是我在开始循环后在代码开头获取的当前帖子ID。
$id = get_the_ID();
html格式如下:
<div class="row">
<div class="col-xs-12">
<h3>CARICA UNA NUOVA IMMAGINE O VIDEO</h3>
<form method="POST" action="" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="postImage" multiple="multiple" class="form-control">
</div>
<input id="uploadImg" name="uploadImgCustom" type="submit" value="AGGIORNA ALLEGATO" class="btn secondary-btn primary-bg">
</form>
</div>
</div>