Support » Plugin: Contact Form DB » Saving File Upload to Ext Location

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    See if this works:

    function cfdbFilterSaveFiles($formData) {
        // CHANGE THIS: CF7 form name you want to manipulate
        $formName = 'your-form';
    
        if ($formData && $formName == $formData->title) {
    
            // CHANGE THIS: directory where the file will be saved permanently
            $uploadDir = '/root/htdocs/wp-content/uploads/path/to/save/dir/';
    
            // CHANGE THIS: URL to the above directory
            $urlDir = 'http://your-site.com/uploads/path/to/save/dir/';
    
            // CHANGE THIS: upload field names on your form
            $fieldNames = array('upload-field-1', 'upload-field-2', 'upload-field-3');
    
            return saveFilesInForm($formData, $uploadDir, $urlDir, $fieldNames);
        }
    
        return $formData;
    }
    
    function saveFilesInForm($formData, $uploadDir, $urlDir, $fieldNames) {
    
        // make a copy of data from cf7
        $formCopy = clone $formData;
    
        foreach ($fieldNames as $fieldName) {
            if (isset($formData->uploaded_files[$fieldName])) {
    
                // breakdown parts of uploaded file, to get basename
                $path = pathinfo($formCopy->uploaded_files[$fieldName]);
                // directory of the new file
                $newfile = $uploadDir . $path['basename'];
    
                // check if a file with the same name exists in the directory
                if (file_exists($newfile)) {
                    $dupname = true;
                    $i = 2;
                    while ($dupname) {
                        $newpath = pathinfo($newfile);
                        $newfile = $uploadDir . $newpath['filename'] . '-' . $i . '.' . $newpath['extension'];
                        if (file_exists($newfile)) {
                            $i++;
                        } else {
                            $dupname = false;
                        }
                    }
                }
    
                // make a copy of file to new directory
                copy($formCopy->uploaded_files[$fieldName], $newfile);
    
                // save the path to the copied file to the cfdb database
                $formCopy->posted_data[$fieldName] = $newfile;
    
                $path = pathinfo($newfile);
                $formCopy->posted_data['file-url'] = $urlDir . $path['basename'];
    
                // delete the original file from $formCopy
                unset($formCopy->uploaded_files[$fieldName]);
            }
    
        }
        return $formCopy;
    }
    
    add_filter('cfdb_form_data', 'cfdbFilterSaveFiles');

    You’re awesome, Michael. This got me very close. All files now save to the custom directory. However, in the database it only saves the new custom URL for the last file upload. Here is a screenshot. Did I miss anything or can you identify what may be missing?

    Thank you!!

    Plugin Author Michael Simpson

    (@msimpson)

    Oh yeah,

    try changing

    $formCopy->posted_data['file-url'] = $urlDir . $path['basename'];
    to

    $formCopy->posted_data[$fieldName . '-url'] = $urlDir . $path['basename'];

    Sincere thanks, Michael – this code works perfectly.

    Could we build on this? If I want to duplicate this filter/action with another form on the site (multiple upload fields again) what would be the best way to achieve this?

    Do you still not recommend duplicating the code for the additional form and instead parameterize? One of the responses in that thread mentioned this didn’t work with new(er) versions of WordPress. What are your thoughts?

    Plugin Author Michael Simpson

    (@msimpson)

    Duplicate the cfdbFilterSaveFiles() function for each form but change the name of the function.

    Add a new line like the following for each form, but change cfdbFilterSaveFiles to the name of the new function you copy.
    add_filter(‘cfdb_form_data’, ‘cfdbFilterSaveFiles’);

    Perfect.

    This was incredibly helpful. Thank you so much. Your plugin and support is second to none, and has been a huge asset to my site(s) over the years.

    I hope this thread helps out others in the future. Can’t do much better than this for a step-by-step tutorial.

    This is awesome, I basically came up with the same approach using a foreach statement in my own implementation!

    One question, how do we make the file url field clickable without hacking the plugin?

    Plugin Author Michael Simpson

    (@msimpson)

    @mkormendy the new URL field will not be clickable in the administration page. If you create a page with a short code to show the data then you can use: http://cfdbplugin.com/?p=867

    I *was* looking for a backend solution…. but now that you mention it, a js front-end solution works just as well.
    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Saving File Upload to Ext Location’ is closed to new replies.