在php中创建可点击链接

时间:2018-06-11 15:55:32

标签: php wordpress contact-form-7

我正在使用wordpress - >联系表单7并使用联系表单7将数据保存到数据库扩展插件。

该插件具有过滤器,可在保存到数据库之前修改数据。 ([赞这个页面])1

现在我想将文件保存到服务器上的不同文件夹中,并将该文件的链接输出到管理面板中。我像这样使用过滤器。

function cfdbFilterSaveFile($formData) {
// CHANGE THIS: CF7 form name you want to manipulate
$formName = 'DemoReport'; 

// CHANGE THIS: upload field name on your form
$fieldName = 'Report'; 

// CHANGE THIS: directory where the file will be saved permanently
$uploaddir = '/home2/username/public_html/example.com/report/wp-content/uploads/reports/';
$urlDir = 'http://example.com/report/wp-content/uploads/reports/';

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;

    $path = pathinfo($newfile);

    $filelink = '<a href=' . $urlDir . $path['basename'] . '>' . $path['basename'] . '</a>'; 

    $formCopy->posted_data[$fieldName . '-url'] = $filelink;

    // delete the original file from $formCopy
    unset($formCopy->uploaded_files[$fieldName]);

    return $formCopy;
}
return $formData; }                              
add_filter('cfdb_form_data', 'cfdbFilterSaveFile');

现在使用此代码,文件将按预期保存到服务器上的文件夹中,但我无法将可单击链接输出到管理面板表中的已保存文件。代替可点击的链接,完整的URL就在那里。如截图所示。

ScreenShot

输出作为完整URL(在屏幕截图中标记为1),而我希望url作为文件的链接输出(屏幕截图中为2)。我试图使用echo()和sprintf,但得到了php syntex错误。

1 个答案:

答案 0 :(得分:0)

感谢您的建议。我找到了输出链接的替代方法。我要做的是在网页上输出表单提交数据,并按照@Ovidash的建议转换javascript可点击的链接...这是我的问题的可接受的解决方法。感谢您的所有建议。

相关问题