• RolandLovesOy

    (@rolandlovesoy)


    This is failing on trying to read the text file contents into a custom field after adding an attachment and I for the life of me can’t figure out why:

    function my_add_attachment_pdftext( $attachment_id ) {
    $attachedfile = basename(get_attached_file($attachment_id));
    $filetype = wp_check_filetype($attachedfile);

    if ($filetype[‘type’] == ‘application/pdf’){

    $fileparts = explode(‘.’, $attachedfile);
    $url = ‘http://www.mydomain.com/’ . $fileparts[0] . ‘.txt’;

    $file = fopen($url, ‘r’);
    $text_data = fread($file, filesize($url));
    fclose($file);

    update_post_meta( $attachment_id, ‘pdftext’, $text_data);
    }
    }
    add_action( ‘add_attachment’, ‘my_add_attachment_pdftext’ );

Viewing 2 replies - 1 through 2 (of 2 total)
  • Andrew Bartel

    (@andrew-bartel)

    Does that work outside of WordPress..?

    Pdfs contain a lot of data, very little of it the actual content.

    Thread Starter RolandLovesOy

    (@rolandlovesoy)

    Ok, just to explain what I was trying to do so it’s clear. After I upload a PDF document, I need to read a text file of the same name (which contains the text from the pdf document) on a web server.

    I figured this out as the file I was trying to read was ‘not readable’ by using fopen. I switched to using CURL and everything worked fine.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Reading Text File Contents Into Custom Field After Adding Attachment’ is closed to new replies.