WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form DB
[Plugin: Contact Form 7 to Database Extension] Database entries (22 posts)

  1. kbarlow
    Member
    Posted 2 years ago #

    Hi and thanks for this wonderful extension.

    I have just one question. How can I get the attachments to save to a directory instead of a BLOB in the DB and replace the blob entry with the link to the download?

    I am just looking around the files but am a bit lost on if this can even be done.

    Thanks =)

    http://wordpress.org/extend/plugins/contact-form-7-to-database-extension/

  2. kbarlow
    Member
    Posted 2 years ago #

    I am sorry this is for contact forms. Forgot to mention that

  3. Michael Simpson
    Member
    Plugin Author

    Posted 2 years ago #

    This plugin does not support that. But when a file is uploaded, it is temporarily saved in the file system. This plugin copies that into the DB. But after that usually the file gets deleted, not but this plugin, but by Contact Form 7 if you use that. So you might see if there is a way to configure CF7 to save the upload somewhere.

  4. jalabiso
    Member
    Posted 1 year ago #

    kbarlow have you find any solution on this? I am looking for the same

  5. jalabiso
    Member
    Posted 1 year ago #

    bytheway Micheal,

    I think the 'storing' part of the job is (at least at the current form) of your plugin, as CF7 orginally is not designed to store anything but just to send.
    Your plugin instead is the one doing the 'saving' now: it gets into the process of CF7, gets the file from TEMP and store it into DB before CF7 deletes it.

    I guess at that point you could decide whether to store it direcly in DB (as you do now) or save it to some dir, saving into DB only its path.

    I hope you don't take this personally. I deeply respect your work and I am just trying to give a suggestion

    keepitup!

  6. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    The way to accomplish this is follow the instructions here to intercept the form when it is about to be saved, then store the file where you like.

  7. euchapelice
    Member
    Posted 1 year ago #

    hi

    i hope you could help, i've tried some filtering of my own

    first, i disabled cf7's "auto" delete attachment (instructions above). then, on instructions from the linked page, i tried adding a field that would contain the cf7 temp dir plus the filename of the attachment, like putting in the database the url instead of the file itself:
    (cf7 temp dir) + filename = url of attachment

    finally, i tried to unset the original field that i thought contained the file (that would be put in the database):
    unset($formData->posted_data['myform-attachment']);

    i even tried to unset 'uploaded_files':
    unset($formData->uploaded_files['fileupload']);

    but my understanding of wp, php and the plugin reached their limit. so my question is: how do i unset or delete the file that's included in the database? so that i'll end up only with the file in the cf7 temp dir and only the url in the database?

    thank you very much! and thank you for this great and awesomely useful plugin!

  8. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    I think you are almost there. Assuming the form's field name for the file upload is "myform-attachment" I think what you need to do is:

    unset($formData->posted_data['myform-attachment']);
    unset($formData->uploaded_files['myform-attachment']);
  9. euchapelice
    Member
    Posted 1 year ago #

    thank you! will try this. once i get this part working, on to the next step: attachment moving (to permanent dir) plus renaming (to avoid overwrites when uploads have the same filename)

  10. euchapelice
    Member
    Posted 1 year ago #

    hello again michael, i was able to try the code you provided, and i was able to save the attachment in the temp dir and store only the link in the database. unfortunately, when i received the test email sent by cf7, there was no attachment. my question is, does the filtering in function.php (for cf2db extension) happen before cf7 sends the form to the recipient email? (thus, attachment gets deleted before it is sent)
    - cf7 form
    - form is filled and processed
    - cf2db gets data and puts these to db
    - cf7 sends form to recipient

    is this the reason why no attachment was sent?

    thanks!

  11. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    my question is, does the filtering in function.php (for cf2db extension) happen before cf7 sends the form to the recipient email? (thus, attachment gets deleted before it is sent)

    You are correct. I didn't consider that. That implies you should not unset the variables.

  12. euchapelice
    Member
    Posted 1 year ago #

    ok, thanks for the quick reply! i'll look into other possible solutions. and share any progress.

    thanks again

  13. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    If you are copying the file to a permanent location, you might simply not unset those variables but add a new variable that references the new file URL

    $formData->posted_data['different-name-attachment'] = 'http://your-site.com/path/to/file';

  14. euchapelice
    Member
    Posted 1 year ago #

    thanks, my first step had something similar. i think i hit a snag where the table/database of cf7todb adds a link of the attachment (a link to the database "blob") to the value of the form-attachment field. i am trying another way, am i correct to think that if i can keep the filename (like, "upload.pdf") without the link part, the database will also not save the blob part? i think saving the attachment part is ok (through commenting out some codes in cf7), the hard part is 'detaching' the blob from "wp_cf7dbplugin_submits" without deleting it before it gets sent by cf7 =)

  15. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    Let me take a step back. Does your filter receive a copy of the form data:
    function myFilter($formData)

    or a reference to it (using "&" notation):
    function myFilter(&$formData)

    because something doesn't make sense here.

    If you use the first form (I figure you probably are), then this filter gets a copy or that data which you can modify and return to CFDB for storing the data, but the CF7 plugin should retain its original unchanged copy. CF7 should then send its email using the original copy and you should still have your attachment (unless you deleted the temp file, which I assume you didn't).

    So I don't see why you would have any issue unset'ing variables or otherwise manipulating $formData in the filter.

    Am I missing something?

  16. euchapelice
    Member
    Posted 1 year ago #

    hi michael, here is the code i added to my function.php (after i comment out the lines in cf7 code, prev):

    function cfdbFilter($formData){
        $formName='Dummy form';
        $uploaddir='http://localhost/smc/wp/wp-content/uploads/wpcf7_uploads'; // note: modify htaccess of "wpcf7_uploads"
        if($formData && $formName == $formData->title){
            $formData->posted_data['form-attachment']=$uploaddir.'/'.$formData->posted_data['form-file'];
            unset($formData->posted_data['form-file']);
            unset($formData->uploaded_files['form-file']);
        }
        return $formData;
    }
    add_filter('cfdb_form_data','cfdbFilter');

    a different approach:
    also, i looked at the code of both plugins, trying to see where the 'write to db' happens (and also the 'send', in the case of cf7). and as with the previous instructions, i commented out code in cf7 and i also tried to comment out this part of "CF7DBPlugin.php":

    if ($filePath) {
        /*$content = file_get_contents($filePath);
        $wpdb->query($wpdb->prepare($parametrizedFileQuery,
                                    $content,
                                    $time,
                                    $title,
                                    $nameClean,
                                    $valueClean));*/
    }

    (lines 520 to 528)

    and

    if (!in_array($field, $foundUploadFiles) && $filePath) {
        /*$fileName = basename($filePath);
        $wpdb->query($wpdb->prepare($parametrizedQuery,
                                    $time,
                                    $title,
                                    $field,
                                    $fileName,
                                    $order++));
        $content = file_get_contents($filePath);
        $wpdb->query($wpdb->prepare($parametrizedFileQuery,
                                    $content,
                                    $time,
                                    $title,
                                    $field,
                                    $fileName));*/
    }

    (lines 536 to 551)

    finally, for the filter in function.php, i just added the path to "form-file" and did not unset anything.

    the result is my desired effect, except of course, it is "ugly" since it tinkers directly with the code of both plugins. i was able to save the attachment from the cf7 form in the filesystem, save the path (and not the file itself) to the cfdb table, and also receive the attachment in the test email.

  17. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    I know you have this working but I think I might have a cleaner way.

    At the beginning of your filter do:

    $copy = clone $formData;
    Then change and unset things on $copy, but leave $formData untouched.

    Rationale:
    As you probably know, it appears that when the filter gets called, $formData is a reference (not a copy) of the data structure that CF7 is using. And this happens just before mail is sent. So any change you make is reflected in the email. In you case this is undesired.

    The CFDB plugin gets $formData from the return value of the filter and stores that in the database. But that doesn't need to be the same copy as what CF7 is using. So the idea is to make a copy (clone) of the CF7 object and modify the copy only for CFDB

    Then you have no need to modify plugin code.

  18. euchapelice
    Member
    Posted 1 year ago #

    hello again, you are right of course =)

    i did as you suggested and the code is much cleaner now, no need to comment out codes inside the two plugins, just the filter code in functions.php, this is what i used:

    function cfdbFilter($formData){
        // make a copy of data from cf7
        $formCopy=clone $formData;
    
        // identify cf7 form you want to manipulate
        $formName='Dummy form';
        // provide directory where the file will be saved permanently
        $uploaddir='/root/htdocs/wp-content/uploads/path/to/attachment/';
    
        if($formCopy && $formName == $formCopy->title){
            // breakdown parts of uploaded file, to get basename
            $path=pathinfo($formCopy->uploaded_files['form-file']);
            // make a copy of file to new directory
            copy($formCopy->uploaded_files['form-file'],$uploaddir.$path['basename']);
            // save the path to the copied file to the cfdb database
            $formCopy->posted_data['form-file']=$uploaddir.$path['basename'];
            // delete the original file from $formCopy
            unset($formCopy->uploaded_files['form-file']);
        }
        return $formCopy;
    }
    add_filter('cfdb_form_data','cfdbFilter');
  19. euchapelice
    Member
    Posted 1 year ago #

    btw, when i was searching the forum for solutions, i found this:
    http://wordpress.org/support/topic/plugin-contact-form-7-to-database-extension-save-files-to-filesystem-not-db?replies=2

    maybe that should link here? for future reference

  20. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    Good idea. I posted a link there.

    If you don't mind me nit-picking your code, I suggest moving the "clone" call inside the "if" statement (and return the clone from inside it, original $formData when "if" fails). As written it aways creates a clone, even when not needed.

  21. euchapelice
    Member
    Posted 1 year ago #

    haha, my limited coding is showing. actually as soon as i tested the filter, i realized that i forgot to include some essential codes, for checking if a file with the same name already exists in the directory. so, here is the (improved) filter:

    function cfdbFilter($formData){
        // identify cf7 form you want to manipulate
        $formName='Dummy form';
    
        if($formData && $formName == $formData->title){
            // make a copy of data from cf7
            $formCopy=clone $formData;
    
            // provide directory where the file will be saved permanently
            $uploaddir='/root/htdocs/wp-content/uploads/path/to/attachment/';
            // breakdown parts of uploaded file, to get basename
            $path=pathinfo($formCopy->uploaded_files['form-file']);
            // 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['form-file'],$newfile);
            // save the path to the copied file to the cfdb database
            $formCopy->posted_data['form-file']=$newfile;
    
            // delete the original file from $formCopy
            unset($formCopy->uploaded_files['form-file']);
    
            return $formCopy;
        }
    }
    add_filter('cfdb_form_data','cfdbFilter');
  22. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    That looks good.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic