简体   繁体   中英

create clickable link in php

I am working with wordpress -> contact form 7 and saving the data with contact form 7 to database extension plugin.

The plugin has filters to modify data before saving into database. ([Like This Page]) 1

now i wanted to save the file into different folder on the server and output link to that file into the admin panel. i used the filter like this.

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');

Now with this code the file is saved into the folder on the server as expected but i am not able to output the clickable link to the saved file in the admin panel tables. In place of clickable links the full url is there. As in the screenshot.

ScreenShot

The output is coming as full URL (as marked 1 in screenshot), while i want the url to output as a link to the file (something like 2 in screenshot). I tried to use echo() and sprintf but got php syntex error.

Thanks for the suggestions. I have found alternate way to output links. What I have to do is output the form submission data on a webpage and convert links clickable by javascript as suggested by @Ovidash ... That is a acceptable workaround for my issue. Thanks for all the suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM