分享经验 版主:论坛审计组
分享一个多文件上传,根据文件类型显示,自定义函数
类型:迅睿CMS 更新时间:2023-03-17 22:35:35 自定义函数 附件扩展名 文件上传

分享一个多文件上传,根据文件类型显示,自定义函数


如果文件是链接也可以判断


函数上传至custom.php


// 检查附件类型

function checkAttachmentType($filename) {

    // 获取附件扩展名

    $extension = pathinfo($filename, PATHINFO_EXTENSION);


    // 判断附件类型

    if (in_array(strtolower($extension), array('jpg', 'jpeg', 'png'))) {

        return 'image';

    } elseif (in_array(strtolower($extension), array('mp3'))) {

        return 'audio';

    } elseif (in_array(strtolower($extension), array('mp4'))) {

        return 'video';

    } elseif (in_array(strtolower($extension), array('ppt', 'pptx'))) {

        return 'ppt';

    } elseif (in_array(strtolower($extension), array('pdf'))) {

        return 'pdf';

    } else {

        return '';

    }

}


// 显示附件

function showAttachment($filename) {

    $attachmentType = checkAttachmentType($filename);


    switch ($attachmentType) {

        case 'image':

            echo '<img src="' . $filename . '" alt="Image">';

            break;


        case 'audio':

            echo '<audio src="' . $filename . '" controls></audio>';

            break;


        case 'video':

            echo '<video src="' . $filename . '" controls></video>';

            break;


        case 'ppt':

            echo '<iframe src="https://view.officeapps.live.com/op/view.aspx?src=' . $filename . '" width="100%" height="500px" frameborder="0"></iframe>';

            break;


        case 'pdf':

            echo '<embed src="' . $filename . '" type="application/pdf" width="100%" height="500px">';

            break;


        default:

            echo 'Invalid attachment type!';

            break;

    }

}





模板调用

{php $filename = dr_get_file($c.file);}

{php showAttachment($filename);}