WordPress.org

Ready to get started?Download WordPress

Forums

Form Manager
Naming uploaded files based on form data (4 posts)

  1. Rockstaru
    Member
    Posted 1 year ago #

    I've set up a form on a website I'm developing for applicants to an opera program to fill out a upload audio recordings. So far it seems to function for the most part. However, I've noticed that when I use the default naming scheme (%filename%) it tends to cause problems when the files are uploaded to the server if, for example, the file has an apostrophe in the name (e.g. "Dio, che nell'alma infondere.mp3" becomes "Dio, che nell\'alma infondere.mp3", which will return a 404 when a user or admin clicks on the file link). The form asks for first and last name; would it be possible to automatically save files as %First Name% %Last Name% Audio Recording 1 (m-d-y-h-i-s).mp3, or something similar?

    Thank you very much; the plugin is absolutely wonderful and has otherwise deployed flawlessly.

    http://wordpress.org/extend/plugins/wordpress-form-manager/

  2. publ2793
    Member
    Posted 1 year ago #

    Rockstaru,

    I'm having the same troubles with file uploads containing apostrophe's. Have you found a solution?

  3. Rockstaru
    Member
    Posted 1 year ago #

    Not an ideal one, but it fixes the problem. In the file.php file (should be under the types folder in the form-manager plugin folder), there is a function that reads as follows:

    protected function getFormattedFileName($uniqueName, $itemInfo){
    $pathInfo = pathinfo($_FILES[$uniqueName]['name']);
    $fileNameFormat = trim($itemInfo['extra']['name_format']) == "" ? get_option( 'fm-file-name-format' ) : trim($itemInfo['extra']['name_format']);
    if($fileNameFormat == "%filename%"){
    $newFileName = $pathInfo['filename'];
    }
    else{
    $fileNamePos = strpos($fileNameFormat, '%filename%');
    if($fileNamePos !== false){
    if($fileNamePos > 0){
    $before = substr($fileNameFormat, 0, $fileNamePos);
    $after = substr($fileNameFormat, $fileNamePos + 10, strlen($fileNameFormat) - $fileNamePos - 10);
    }
    else{
    $before = "";
    $after = substr($fileNameFormat, -1 * (strlen($fileNameFormat) - 10));
    }
    }
    $newFileName = "";
    if($before != "")
    $newFileName.= fm_get_time($before);
    if($fileNamePos !== false)
    $newFileName.= $pathInfo['filename'];
    if($after != "")
    $newFileName.= fm_get_time($after);
    }
    $newFileName.= '.'.$pathInfo['extension'];

    return $newFileName;
    }

    Add the following before the final "return $newFileName;"

    $newFileName = preg_replace("/[^A-Za-z0-9_-\s.]/", "", $newFileName);

    This essentially deletes all non-alphanumeric characters and spaces from the file name (and preserves periods, so the filetype is preserved). In the above example, "Dio, che nell'alma infondere.mp3" becomes "Dio che nellalma infondere.mp3." Again, not an ideal solution, but it works.

  4. publ2793
    Member
    Posted 1 year ago #

    Wow - thank-you for helping me here. I'm not tech-qualified enough to make this adjustment, but my pastor helped me with some other coding, and I think he'll do this for me too...

    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic