• 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="http://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://ww.wp.xz.cn/plugins/contact-form-7-to-database-extension/

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

    (@msimpson)

    The field value should be getting overwritten to the full path of the file via:

    $formCopy->posted_data[$fieldName] = $newfile;

    But that’s not a URL.

    Right after that you could add lines:

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

    to create a URL field.

    But I’m not sure how that looks in the email. Haven’t tried it in a long while.

    Thread Starter Magee

    (@magee)

    in the email it just outputs the file name before it gets changed for some reason. so every email with the original file uploaded called “let’s say – image.jpg” ends up with the right entry and image address in the database but the wrong image in the email because [Photo] dies not get overwritten before sending the email, or never.

    Does it make sense?

    Plugin Author Michael Simpson

    (@msimpson)

    I’m thinking it might be a lot easier to do the following in the code

    – Don’t save the files in the same directory, create a subdirectory for each one
    – Remove the code to rename the file

    Benefits are:
    – More than one file can have the same name
    – Name and attachment that CF7 puts in the email will be consistent

    Thread Starter Magee

    (@magee)

    Hi Michael,

    could you help me out? I mean I’m ok at programming in general but terrible with php. I just need the file copied in a filder and than the database to refer to the correct file AND the email to output the correct url in the tag [Photo] as at the moment it just tells me the name and If i copy the file in a new folder I won’t be able to hardcode the address in the email body as I do now.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘send changed file cf7’ is closed to new replies.