dow57588 2015-11-04 16:10
浏览 18

创建/调用函数,所以我不必多次插入相同的代码?

some background : My webste integrates a File Manager (FileGator) that allows users to consult the files present in it, and also functionnalities like upload/move/create or delete files. The code I'm working on determines the privileges of the current user and only displays the functionnalities if he has the rights privileges.

When a user registers on the website, a repertory $userdir is created in which the member is granted functionnalities privileges. The code that allows me to display a functionnality for the user checks if one of thoses conditions is met :

  • User has full rights
  • User has normal rights but is on his main repertory ( /user/$userdir )
  • User has normal rights but is on one of the subfolders of his main repertory ( /user/$userdir/subfolder1 )

So currently I have to repeat the full code for every functionnality (about eight).

Here is my code, with the upload functionnality :

<?php
    $directory = $_SESSION['cwd'];                  // current directory
    $user = $_SESSION['simple_auth']['username'];   // get username
    $repository = gatorconf::get('repository');     // get base repertory of the file manager
    $userdir = $repository.DS.'user'.DS.$user;      // user's repertory

    function scanDirectory($userdir = '', $directory){
        $folders = glob($userdir . '/*' , GLOB_ONLYDIR);
        foreach($folders as $folder){
            if (($folder == $directory && gator::checkPermissions('r')) || (gator::checkPermissions('ru')) || ($userdir == $directory && gator::checkPermissions('r'))): ?>
                <!-- The fileinput-button span is used to style the file input field as button -->
                <span class="fileinput-button nice radius button">
                <i class="icon-plus icon-white"></i>

                <span class=""><?php echo lang::get("Add Files...")?></span>

                <input type="file" name="files[]" multiple>
                <input type="hidden" name="uniqid" value="50338402749c1">
                </span>
<?php break; endif;
    scanDirectory($folder, $directory);
        }
    }

    scanDirectory($userdir, $directory);
?>

The only part of this code that changes is the HTML code inside the If condition, in my exemple this was the HTML to display the Upload functionnality. The code works fine but currently the same code is repeated for every functionnality in the page, while only the HTML part of the code is changing.

I'm still new in php and I was wondering if there is a way to simplify this, make the code less redundant.

Is there a way to transorm this into some kind of function ? Like write the full code once in a function.php file and then call it on my main page where i would only add he code of the functionnality.

Hope this is clear enough since I'm too sure if that is possible.

Thanks !

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计