PDF Upload from Input Error
I have a custom plugin that creates an input field under each post, that allows users to upload PDF files.
$html .= 'input accept=application/pdf type=file id=wp_custom_attachment name=wp_custom_attachment value= size=25';
I am using wp_upload_bits
and wp_insert_attachment
to upload the file:
if(!empty($_FILES)) {
if ( !empty($_FILES['wp_custom_attachment']['name'])) {
$uploaded_anexa = wp_upload_bits($_FILES['wp_custom_attachment']['name'], null, file_get_contents($_FILES['wp_custom_attachment']['tmp_name']));
$uploaded_anexa_location = trailingslashit ( $wp_upload_dir['path'] ) . $_FILES['wp_custom_attachment']['name'];
$attachment_anexa = array(
guid' = trailingslashit ($wp_upload_dir['url']) . basename( $uploaded_anexa_location ),
'post_type' = 'attachment',
'post_mime_type' = $_FILES['wp_custom_attachment']['type'],
'post_title' = preg_replace('/\.[^.]+$/', '', basename( $_FILES['wp_custom_attachment']['name'] )),
'post_content' = $_POST['_wpd_sub_title_title'],
'post_status' = 'inherit',
'post_category' = array( 0, $id ),
'post_excerpt' = $numar_inregistrare,
);
$attach_id = wp_insert_attachment( $attachment_anexa, $uploaded_anexa_location, 37);
array_push($files, $uploaded_anexa_location);
}
The problem is that this code only works with certain PDF files. For instance, if I try to upload a PDF file created with Word - Create PDF I get this error:
Warning: fopen(/var/www/html/wp-content/uploads/2022/01/Anexa dispozitie.pdf): failed to open stream: No such file or directory in /var/www/html/wp-content/plugins/print-post-to-pdf/FPDI/src/PdfParser/StreamReader.php on line 42 Fatal error: Uncaught InvalidArgumentException: No stream given.
If I convert the file with Mac OS's Preview and generate a new PDF (with the same content) it works just fine and the file gets uploaded.
Can you please give me your opinions on how I can address this issue? Thanks!
Topic uploads attachments pdf Wordpress
Category Web