• Hi Guys,

    I’m struggling a bit with a dilemma.
    My client has a registration form “Registration 1” that has a file attachment [Photo].

    This is hooked to contact form 7 db. They have a page where they check at all the subscriptions.

    As the did not wish to receive the attachment as a file attached to the form but as part of an html output, i couldn’t link the database address that contact form usually uses so i changed the location of the file with the following function in the function.php

    function cfdbFilterSaveFile($formData)
    {
        // CHANGE THIS: CF7 form name you want to manipulate
        $formName = 'Registration 1'; 
    
        // CHANGE THIS: upload field name on your form
        $fieldName = 'Photo'; 
    
        // CHANGE THIS: directory where the file will be saved permanently
        $uploaddir = '/home/dir/public_html/wp-content/uploads/cf7/';
    
        if ($formData && $formName == $formData->title && isset($formData->uploaded_files[$fieldName])) {
            // make a copy of data from cf7
            $formCopy = clone $formData;
    
            // 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;
    
            // delete the original file from $formCopy
            unset($formCopy->uploaded_files[$fieldName]);
    
            return $formCopy;
        }
        return $formData;
    }
    
    add_filter('cfdb_form_data', 'cfdbFilterSaveFile');

    The file gets saved and if there is a duplicate it changes the name.

    GREAT!! Now the output in the email is the following

    <table width="120px" bgcolor="#fff" cellpadding="10px">
    <tr>
        <td class="details photo" valign="top"><img src="https://website.com.au/wp-content/uploads/cf7/[Photo]" width="120" style="width:120px"/></td>
      </tr>
    </table>

    and it works fine to a level: if 2 people upload a file with the same name, the function above will copy and change it to another name but cf7 will still output the name of the original file uploaded as [Photo] resulting the email linking to the wrong file because the change of name does not happen to the field [Photo] per se. Is there a way I can get the new url to output in the [Photo] field?

    This is giving me a massive headache.

    Thanks so much for your help !!!

    https://www.ads-software.com/plugins/contact-form-7/

  • The topic ‘Attachment and CF7DB’ is closed to new replies.