Retrieve Upload Filename and Path

Question:

Does anyone know how to return the filename with path for a File Upload field?

Nick Amis, Formidable Slack Community

This is a simple question, but the answer is somewhat elusive.

File Upload Field

First, let's gain an understanding of Formidable's file upload field. For one reason or another, the file upload field has had gone through several revisions over the course of its history. In its current incarnation, it uses the popular DropzoneJS library to drive its functionality. DropzoneJS is an open source library that provides drag’n’drop file uploads with image previews. It is lightweight and doesn’t depend on any other library. It's also highly customizable.

When you upload a file through the file upload field, it is immediately uploaded to a directory on your server. Formidable creates its own directory for uploaded files, which you can find under wp-content/uploads/formidable.

When you click the submit button on a form with the file upload field, any uploaded files are added to the WordPress media library. Normally, if you add files to the media library, they are added to the standard WordPress directory structure. But this isn't the case with Formidable's file upload field. Formidable's uploaded files remain in the Formidable directory structure and an entry is created in the wp_posts table for the media library item.

Let's analyze how Formidable manages the database transactions. First, we'll query the wp_frm_item_metas table in PHPMyAdmin and search for a file upload field.

If you're not familiar with Formidable's table structure, see this knowledge base article describing the database schema.

The SQL query returns the following results:

SQL query result set in PHPMyAdmin
Results of SELECT * FROM `wp_frm_item_metas` where field_id = '651'

We want to examine the meta value in this result set. The meta value for upload fields is a pointer to the record id in wp_posts that is created the form entry is saved. This means that if we want to retrieve the filename and path, we have to look into wp_posts for the targeted record, and this is what we see:

Result set for wp_posts query in PHPMyAdmin
Result set for SELECT * FROM `wp_posts` where id = '769'

To find the filename and path, we have to scroll to the right:

Result set from wp_posts showing guid field
This section of the result set showing the guid.

The guid, or 'Globally Unique Identifier', contains the filename and path and for this media library entry. In this case, it's:

http://localhost/public_html/wp-content/uploads/formidable/32/calendar-1847346.svg

Answer:

In order to retrieve the the filename and path for the uploaded article, you have to first get the record id from wp_frm_item_metas, then use that value as the parameter passed to the WordPress get_the_guid() function.

Multiple File Uploads

If you have your upload field configured to "Allow multiple files to be uploaded", the process is a little different because Formidable stores the field's meta_value differently in wp_frm_item_metas. For multiple files, the meta_value is stored as a PHP serialized array:

a:2:{i:1;i:797;i:2;i:798;}

Serialized arrays need to be unserialized before you can use them. You can then loop through the resulting data set to retrieve each uploaded file's guid.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

  1. Thanks for the thorough explanation guys.

    In the end I used the WordPress ‘get_attached_file’ to retrieve the filename and path which seemed a lot simpler than querying the database.

    $entry = FrmEntry::getOne($entry_id, true);
    $attach_id = $entry->metas[588];
    $filename = get_attached_file($attach_id);

    • The one thing I’ve learned about programming is that there is usually more than one way to achieve a goal. Thanks for sharing your approach. However, there’s quite a difference between using get_attached_file() and get_the_guid(). The returned values are not the same and the difference can cause some head scratching depending on what the requirement is that you are attempting to resolve. Let me explain…

      The original poster wanted to return the file path and name to display the image in an email or on a page. In order to display an image, you need the file’s path and name returned as a URL for the img src. In this instance, the wp_posts guid is the file’s URL. The WordPress get_attached_file() function returns a hard disk path. It is not a URL and requires additional manipulation to be used as one.

      You also said that you used get_attached_file() instead of querying the database. I think you may have misunderstood the point of the article here. Nowhere in this article are there any suggestions about directly querying the database to return the filename to accomplish a goal. The displayed database query results are part of the problem analysis, not the recommended solution. The suggested approach to achieve the original poster’s requirement is to use the WordPress get_the_guid() function. Besides, it’s not possible to return any data without querying the database. The FrmEntry::getOne() function executes a database query and so does get_attached_file() and get_the_guid() for that matter. Regardless of the approach, you are querying the database.

      I tested your approach on my local development machine. I uploaded a small .mp3 file and these are the results of get_attached_file() vs. get_the_guid():

      get_attached_file() = “/Users/victorfont/Sites/public_html/wp-content/uploads/formidable/32/mister-softee-jingle.mp3”
      get_the_guid() = “http://localhost/public_html/wp-content/uploads/formidable/32/mister-softee-jingle.mp3”

      Which of these two results are more useful for displaying the file on a page, post, or view?