• Been going round in circles for way too long with walking back my code, adding debug statements, and still have no clue what is happening here. Things that come to mind are: Data Race conditions, Cache vs non Cache processing, Other unknown? The code I have written appears to be working as designed but the expected functionality is still broken

    Using a FORM BUILDER Plugin that provides the ability to upload files which in turn are emailed as attachments … as the uploaded filenames are random, wanting to rename them automatically to simplify downstream email processing.

    The Form Builder provides a before email hook filter which I am attempting to use to rename the uploaded files before they are emailed as attachments.

    Added a FORMID-POSTID Prefix to all attachments PLUS each attachment has a unique Fixed name defined by the form’s field details … all attachment files simply get renamed to:
    SomeRandomFilename.SomeAcceptablePrefix -> xFORMID-POSTID-FIELDNAME.SomeAcceptablePrefix

    The hook logic written in PHP queries all relevant records in posts & postmeta tables followed by successfully renaming all appropriate files then finally updates the relevant records.

    Files are renamed using builtin PHP functions: glob() and rename() … files all appear to be renamed properly

    posts and postmeta Tables are updated using builtin PHP functions: str_replace() with:
    $wpdb->update(table,array(‘data_field’ => $data),array(‘ref_field’ => $ref)

    while the filename data appears to be updated properly in both tables, for some reason the email processor is randomly ignoring some of the files. With renaming turned off, testing with 5 uploaded files, all 5 files are emailed. Same test with renaming turned on, files are randomly missing … review of folders, log files, and data shows that all files and records were updated correctly … plus … there are no error_log entries.

    I went looking for wp functions to doing meta records updates in case there may be some inherent checksums that I am missing but maybe not using the right questions … regardless … if this was a checksup issue I wouldn’t expect ANY emailed files … if this is a race condition with the wpdb->update() function then perhaps I have to add a wait loop of some sort … if this is a CACHE issue where the Plugin is using previous data then I may have to build my own MAILER routine.

    Thanks in advance for ideas

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The fact renaming sometimes works pretty much discounts almost all expected issues like cache, checksum, coding errors, etc. All except race condition. Randomly working or not is a classic race symptom. The way multi-threading, pipelining processors work, a simple delay loop in a filter callback may not suffice. What really should happen is something concrete is verified before the mail function is actually called.

    If wp_mail() is used to send emails, the ‘phpmailer_init’ action fires just before PHPMailer::send() is called. This would be the place to check if the renamed file exists yet, and to keep checking (within reason) until it does. For servers still using spinning disks, we can possibly reach this point in code before the renaming is actually recorded on disk, e.g. a race condition.

Viewing 1 replies (of 1 total)
  • The topic ‘PHP Action Hook/Filter to rename media file problem’ is closed to new replies.