media_handle_upload creates two entries
-
I have the following code to upload PDFs to a secure file location, i.e.: outside the public html directory structure. But it creates a duplicate entry and I don’t see where or how it is happening.
// ajax report upload
function mycode_report_upload() {
// set the upload directory
add_filter( 'upload_dir', 'mycode_support_custom_upload_dir' );
// Security check (nonce verification)
//if(!check_ajax_referer('mycode_report_upload', 'nonce')){
// wp_die('Invalid security check, unable to proceed!');
//}
$secure_dir_name = 'mycode_secure_files';
$report_nice_name = sanitize_text_field($_POST['report-name']);
$school_board = sanitize_text_field($_POST['school-board']);
$report_type = sanitize_text_field($_POST['report-type']);
$file_date = sanitize_text_field($_POST['mycode-file-date']);
$target_path = dirname(__DIR__, 5);
$secure_dir = $target_path . DIRECTORY_SEPARATOR . $secure_dir_name . DIRECTORY_SEPARATOR . $school_board . DIRECTORY_SEPARATOR . $report_type;
$base_file_name = ! empty( $_FILES['mycode-file-upload'] ) ? basename($_FILES['mycode-file-upload']['name']) : '';
$report_name = ! empty( $_FILES['mycode-file-upload'] ) ? $_FILES['mycode-file-upload']['name'] : '';
$report_name_no_ext = pathinfo($_FILES['mycode-file-upload']['name'], PATHINFO_FILENAME);
if ( ! empty( $_FILES['mycode-file-upload'] ) ) {
if(! is_dir($secure_dir) ){
if (!mkdir($secure_dir, 0755, true)) {
// Force delete the attachment (bypasses the trash)
$deleted = wp_delete_attachment( $attachment_id, true );
wp_send_json_error('Failed to create directories...');
wp_die();
}
}
// check if file already exists
$file_exists = check_file_already_exists($secure_dir, $base_file_name );
if(! $file_exists){
// upload file
$attachment_id = media_handle_upload( 'mycode-file-upload', 0 );
// move file to secure directory
$moved = rename(get_attached_file( $attachment_id ), $secure_dir . DIRECTORY_SEPARATOR . $base_file_name);
if($moved){
chmod($secure_dir . DIRECTORY_SEPARATOR . $base_file_name, octdec(755));
}
}else {
// get existing file id
$upload_file = $_FILES['mycode-file-upload'];
$file_data = array(
'name' => $upload_file['name'],
'type' => $upload_file['type'],
'tmp_name' => $upload_file['tmp_name'],
'error' => $upload_file['error'],
'size' => $upload_file['size']
);
$arr = wp_handle_upload($file_data, array('test_form' => false));
$moved = rename($arr['file'], $secure_dir . DIRECTORY_SEPARATOR . $base_file_name);
if($moved){
chmod($secure_dir . DIRECTORY_SEPARATOR . $base_file_name, octdec(755));
}
$attachment_id = get_existing_file_id($school_board, $report_type, $report_name_no_ext);
}
set_post_type( $attachment_id, 'mycode-secured-file' );
// set attachment details
$args = array(
'ID' => $attachment_id,
'post_title' => $report_nice_name,
'post_content' => $report_nice_name,
'comment_status' => 'closed',
'post_date' => date('Y-m-d H:i:s', strtotime($file_date)),
'post_date_gmt' => date('Y-m-d H:i:s', strtotime($file_date)),
'post_status' => 'secured',
);
// update post
wp_update_post($args);
// update meta value - > by attachment id _wp_attached_file
update_post_meta( $attachment_id, '_wp_attached_file' , $secure_dir . DIRECTORY_SEPARATOR . $base_file_name);
// add school board and report type post meta
update_post_meta( $attachment_id, 'mycode_school_board', $school_board);
update_post_meta( $attachment_id, 'mycode_report_type', $report_type);
update_post_meta( $attachment_id, 'mycode_file_date', date('Y-m-d H:i:s', strtotime($file_date)));
update_post_meta( $attachment_id, 'mycode_report_path', $secure_dir . DIRECTORY_SEPARATOR . $base_file_name);
wp_send_json_success('File Uploaded Successfully!');
}else{
wp_send_json_error('File is Empty!');
}
//remove the upload directory create process
remove_filter( 'upload_dir', 'mycode_support_custom_upload_dir' );
wp_die();
}Any advice is welcome, but I mainly want to stop the second record from being generated.
Thanks
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
You must be logged in to reply to this topic.