{"id":794,"date":"2012-11-24T17:39:36","date_gmt":"2012-11-24T22:39:36","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=794"},"modified":"2015-12-04T10:46:19","modified_gmt":"2015-12-04T15:46:19","slug":"save-file-to-alternate-location","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=794","title":{"rendered":"Save file to alternate location"},"content":{"rendered":"<p>Common issue: the plugin saves files that are submitted via forms to the database&#8230;but I want to save the file to a special directory.<\/p>\n<p>To do this, you create a filter as described on the page about <a href=\"http:\/\/cfdbplugin.com\/?page_id=747\">Changing Form Data Before it is Saved<\/a> but you use the following code.<\/p>\n<p>Change the values of <strong>$formName<\/strong>, <strong>$fieldName<\/strong> and <strong>$uploaddir<\/strong> to suit your needs.<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">function cfdbFilterSaveFile($formData)\r\n{\r\n    \/\/ CHANGE THIS: CF7 form name you want to manipulate\r\n    $formName = 'your-form'; \r\n    \r\n    \/\/ CHANGE THIS: upload field name on your form\r\n    $fieldName = 'form-file'; \r\n\r\n    \/\/ CHANGE THIS: directory where the file will be saved permanently\r\n    $uploaddir = '\/root\/htdocs\/wp-content\/uploads\/path\/to\/save\/dir\/';\r\n\r\n    if ($formData &amp;&amp; $formName == $formData-&gt;title &amp;&amp; isset($formData-&gt;uploaded_files[$fieldName])) {\r\n        \/\/ make a copy of data from cf7\r\n        $formCopy = clone $formData;\r\n        \r\n        \/\/ breakdown parts of uploaded file, to get basename\r\n        $path = pathinfo($formCopy-&gt;uploaded_files[$fieldName]);\r\n        \/\/ directory of the new file\r\n        $newfile = $uploaddir . $path['basename'];\r\n\r\n        \/\/ check if a file with the same name exists in the directory\r\n        if (file_exists($newfile)) {\r\n            $dupname = true;\r\n            $i = 2;\r\n            while ($dupname) {\r\n                $newpath = pathinfo($newfile);\r\n                $newfile = $uploaddir . $newpath['filename'] . '-' . $i . '.' . $newpath['extension'];\r\n                if (file_exists($newfile)) {\r\n                    $i++;\r\n                } else {\r\n                    $dupname = false;\r\n                }\r\n            }\r\n        }\r\n\r\n        \/\/ make a copy of file to new directory\r\n        copy($formCopy-&gt;uploaded_files[$fieldName], $newfile);\r\n        \/\/ save the path to the copied file to the cfdb database\r\n        $formCopy-&gt;posted_data[$fieldName] = $newfile;\r\n\r\n        \/\/ delete the original file from $formCopy\r\n        unset($formCopy-&gt;uploaded_files[$fieldName]);\r\n\r\n        return $formCopy;\r\n    }\r\n    return $formData;\r\n}\r\n\r\nadd_filter('cfdb_form_data', 'cfdbFilterSaveFile');\r\n<\/pre>\n<h3>A More Sophisticated Example<\/h3>\n<p>Example where we save the file and add a URL to the file location into the form submission. Based on <a href=\"https:\/\/wordpress.org\/support\/topic\/saving-file-upload-to-ext-location?replies=8#post-7640847\">forum discussion<\/a>. <\/p>\n<p>This example is good if you have multiple forms to save file from. Here we have a reusable function &#8220;saveFilesInForm&#8221; and a &#8220;cfdbFilterSaveFiles&#8221; function that calls it. If you have multiple forms:<\/p>\n<ol>\n<li>create a new version of &#8220;cfdbFilterSaveFiles&#8221; with a unique function name for each form<\/li>\n<li>Add a new &#8220;add_filter&#8221; call for each form, passing the name of the new function<\/li>\n<li>The &#8220;saveFilesInForm&#8221; function appears only once<\/li>\n<\/ol>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">function cfdbFilterSaveFiles($formData) {\r\n    \/\/ CHANGE THIS: CF7 form name you want to manipulate\r\n    $formName = 'your-form';\r\n\r\n    if ($formData && $formName == $formData->title) {\r\n\r\n        \/\/ CHANGE THIS: directory where the file will be saved permanently\r\n        $uploadDir = '\/root\/htdocs\/wp-content\/uploads\/path\/to\/save\/dir\/';\r\n\r\n        \/\/ CHANGE THIS: URL to the above directory\r\n        $urlDir = 'http:\/\/your-site.com\/uploads\/path\/to\/save\/dir\/';\r\n\r\n        \/\/ CHANGE THIS: upload field names on your form\r\n        $fieldNames = array('upload-field-1', 'upload-field-2', 'upload-field-3');\r\n\r\n        return saveFilesInForm($formData, $uploadDir, $urlDir, $fieldNames);\r\n    }\r\n\r\n    return $formData;\r\n}\r\n\r\nadd_filter('cfdb_form_data', 'cfdbFilterSaveFiles');\r\n\r\n\r\nfunction saveFilesInForm($formData, $uploadDir, $urlDir, $fieldNames) {\r\n\r\n    \/\/ make a copy of data from cf7\r\n    $formCopy = clone $formData;\r\n\r\n    foreach ($fieldNames as $fieldName) {\r\n        if (isset($formData->uploaded_files[$fieldName])) {\r\n\r\n            \/\/ breakdown parts of uploaded file, to get basename\r\n            $path = pathinfo($formCopy->uploaded_files[$fieldName]);\r\n            \/\/ directory of the new file\r\n            $newfile = $uploadDir . $path['basename'];\r\n\r\n            \/\/ check if a file with the same name exists in the directory\r\n            if (file_exists($newfile)) {\r\n                $dupname = true;\r\n                $i = 2;\r\n                while ($dupname) {\r\n                    $newpath = pathinfo($newfile);\r\n                    $newfile = $uploadDir . $newpath['filename'] . '-' . $i . '.' . $newpath['extension'];\r\n                    if (file_exists($newfile)) {\r\n                        $i++;\r\n                    } else {\r\n                        $dupname = false;\r\n                    }\r\n                }\r\n            }\r\n\r\n            \/\/ make a copy of file to new directory\r\n            copy($formCopy->uploaded_files[$fieldName], $newfile);\r\n\r\n            \/\/ save the path to the copied file to the cfdb database\r\n            $formCopy->posted_data[$fieldName] = $newfile;\r\n\r\n            $path = pathinfo($newfile);\r\n            $formCopy->posted_data[$fieldName . '-url'] = $urlDir . $path['basename'];\r\n\r\n            \/\/ delete the original file from $formCopy\r\n            unset($formCopy->uploaded_files[$fieldName]);\r\n        }\r\n\r\n    }\r\n    return $formCopy;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Common issue: the plugin saves files that are submitted via forms to the database&#8230;but I want to save the file to a special directory. To do this, you create a filter as described on the page about Changing Form Data Before it is Saved but you use the following code. Change the values of $formName, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":102,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-794","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-cO","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=794"}],"version-history":[{"count":18,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/794\/revisions"}],"predecessor-version":[{"id":1348,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/794\/revisions\/1348"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/102"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}