wp_delete_attachment、wp_delete_file_from_directory 和 wp_delete_file() 不会从服务器删除文件
wp_delete_attachment, wp_delete_file_from_directory and wp_delete_file() does not delete file from server
每当 post 正在 saved/updated 时,我正在尝试重新生成 PDF 文件。 PDF 文件名根据 post_meta 数据更改,因此我想在 post 为 saved/updated 时从服务器删除现有的 PDF 附件和文件,然后再重新生成和附加 pdf .
wp_delete_attachment() 可以删除附件,但即使强制删除,文件仍保留在服务器上。
我也试过wp_delete_file_from_directory($file, $path); returns 确实删除了文件,但文件保留在服务器上。 wp_delete_file();
相同
唯一似乎起作用的是 unlink(),但这会产生另一个问题,因为万一文件名没有改变,unlink() 似乎会停止创建具有相同名称的文件名字.
wp_update_post( $my_post );
if(get_post_status( $post_id ) == "publish"):
$existing_PDFs = get_attached_media('application/pdf', $post_id);
foreach($existing_PDFs as $pdf):
$file = get_attached_file($pdf->ID, true);
$path = pathinfo($file, PATHINFO_DIRNAME);
wp_delete_file_from_directory( $file, $path);
wp_delete_file( $file );
wp_delete_attachment($pdf->ID, true);
endforeach;
include('generate-single-machine-pdf.php');
endif;
让 Wordpress 从服务器上删除文件和附件的秘诀是什么?
解决方案是在 wp_delete_attachment 之后有条件地使用取消链接,以防生成的 PDF 与删除的 PDF 同名。在这种情况下,不需要删除附件或取消链接,只需覆盖现有文件即可。
更新帖子:
$savePath = trailingslashit($uploads_dir).$new_file_name.'.pdf';
$pdf->Output($savePath, 'F');
$existing_PDF = get_attached_media('application/pdf', $post_id);
foreach($existing_PDF as $pdf):
$oldPDFID= $pdf->ID;
$file = get_attached_file($oldPDFID, true);
$old_file_name = pathinfo($file,PATHINFO_BASENAME);
endforeach;
}
$old_pdf = pathinfo($file,PATHINFO_BASENAME);
#######CREATE PDF ATTACHMENT####
$args = array(
'post_title' => "$new_file_name",
'post_content' => '',
'post_mime_type' => 'application/pdf',
);
$new_file_name = $new_file_name.".pdf";
//DELETE OLD ATTACHMENT
if($new_file_name != $old_file_name):
wp_delete_attachment($oldPDFID, true);
unlink($file);
endif;
$pdfID = wp_insert_attachment($args, $savePath, $post_id);
每当 post 正在 saved/updated 时,我正在尝试重新生成 PDF 文件。 PDF 文件名根据 post_meta 数据更改,因此我想在 post 为 saved/updated 时从服务器删除现有的 PDF 附件和文件,然后再重新生成和附加 pdf .
wp_delete_attachment() 可以删除附件,但即使强制删除,文件仍保留在服务器上。
我也试过wp_delete_file_from_directory($file, $path); returns 确实删除了文件,但文件保留在服务器上。 wp_delete_file();
相同唯一似乎起作用的是 unlink(),但这会产生另一个问题,因为万一文件名没有改变,unlink() 似乎会停止创建具有相同名称的文件名字.
wp_update_post( $my_post );
if(get_post_status( $post_id ) == "publish"):
$existing_PDFs = get_attached_media('application/pdf', $post_id);
foreach($existing_PDFs as $pdf):
$file = get_attached_file($pdf->ID, true);
$path = pathinfo($file, PATHINFO_DIRNAME);
wp_delete_file_from_directory( $file, $path);
wp_delete_file( $file );
wp_delete_attachment($pdf->ID, true);
endforeach;
include('generate-single-machine-pdf.php');
endif;
让 Wordpress 从服务器上删除文件和附件的秘诀是什么?
解决方案是在 wp_delete_attachment 之后有条件地使用取消链接,以防生成的 PDF 与删除的 PDF 同名。在这种情况下,不需要删除附件或取消链接,只需覆盖现有文件即可。
更新帖子:
$savePath = trailingslashit($uploads_dir).$new_file_name.'.pdf';
$pdf->Output($savePath, 'F');
$existing_PDF = get_attached_media('application/pdf', $post_id);
foreach($existing_PDF as $pdf):
$oldPDFID= $pdf->ID;
$file = get_attached_file($oldPDFID, true);
$old_file_name = pathinfo($file,PATHINFO_BASENAME);
endforeach;
}
$old_pdf = pathinfo($file,PATHINFO_BASENAME);
#######CREATE PDF ATTACHMENT####
$args = array(
'post_title' => "$new_file_name",
'post_content' => '',
'post_mime_type' => 'application/pdf',
);
$new_file_name = $new_file_name.".pdf";
//DELETE OLD ATTACHMENT
if($new_file_name != $old_file_name):
wp_delete_attachment($oldPDFID, true);
unlink($file);
endif;
$pdfID = wp_insert_attachment($args, $savePath, $post_id);