【问题标题】:Show and Hide Button (href=) if file exist in server or not如果文件存在于服务器中,则显示和隐藏按钮 (href=)
【发布时间】:2019-04-27 02:06:53
【问题描述】:

我尝试编辑一个类似的解决方案,但它没有达到预期的效果。我不确定我的编码可能是错误的并且我使用的是 html。如果文件存在,有没有办法隐藏按钮(href =)然后显示它是否找不到?谢谢!

参考:link

<button type="button" id="test_btn" style="display: none;">Download</button>

<script type="text/javascript">
    $(document).ready(function () {
        checkFile();

        function checkFile() {
            $.ajax({
                url: '/path/to/file_checker.php',
                type: 'GET',
                success: function (data) {
                    if (data === "deleted") { 
                        $('#test_btn').show();
                    }
                    else {
                        $('#test_btn').hidden();
                    }
                }
            });
        }
    }
</script>  

那么您的文件检查器 php 可以与您所拥有的类似:

if (file_exists("/aaa/file.txt")) {
    echo "exists";
}
else {
    echo "deleted";
}

【问题讨论】:

    标签: php css display


    【解决方案1】:

    另一种方法是向用户提供现有文件的列表并允许他们从列表中进行选择。

    <?php
    // Initialize an empty array
    $files = [];
    
    // Get all the files in a directory (this will also return directories)
    foreach (glob('*') as $f) {
    
            // If the item is a file, add it to the files array
            if (is_file($f)) {
                    $files[] = $f;
            }
    }
    ?>
    
    <!-- Create a select statement with the files that exist -->
    <select id="files">
    <!-- Placeholder -->
    <option disabled selected>Select file</option>
    
    <!-- Loop through all the files and create options for them -->
    <?php foreach ($files as $f) : ?>
    <option><?= $f ?></option>
    <?php endforeach ?>
    </select>
    
    <!-- The button would run the download using the selected file as the source (additional JavaScript required) -->
    <button id="go">Download</button>
    

    【讨论】:

    • 哇,太好了!我将尝试学习如何实现这一点。非常感谢!
    【解决方案2】:
    <script
                  src="https://code.jquery.com/jquery-3.3.1.min.js"
                  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
                  crossorigin="anonymous">
    
    </script>
    

    将 CSS 类添加到您的按钮并在您的页面中包含 jquery

    .hidden {
    display: none
    }
    

    然后

    <button type="button" class="hidden" id="test_btn">Download</button>
    
    <script>
    $(document).ready(function () {
                    $.ajax({
                    url: '/path/to/file_checker.php',
                    type: 'GET',
                    success:function(data){
                    var obj = jQuery.parseJSON(data);
                               if(obj.callback == 1) {
                                 $('button#test_btn').removeClass('hidden');
                                }
                    }
                    });
    });
    </script>
    

    在您的 /path/to/file_checker.php 中

    if (file_exists("/aaa/file.txt")) {
        $data = array('callback' => 1);
        echo json_encode($data);
    }
    else {
        $data = array('callback' => 0);
        echo json_encode($data);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2019-12-15
      • 1970-01-01
      • 2014-05-03
      • 2022-08-11
      • 1970-01-01
      相关资源
      最近更新 更多