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 = 'https://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');