服务器上文件存在时不显示按钮 [英] Don't display a button while file exists on server

查看:274
本文介绍了服务器上文件存在时不显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅在删除网络服务器上的特定文件时才尝试在HTML页面上显示下载按钮。我以为我会使用CSS display:none; 然后是带有while循环的PHP脚本,如下所示:

I'm trying to display a download button on an HTML page only when a specific file on the web server is deleted. I thought I'd use a CSS display: none; then a PHP script with a while loop that'd look like this :

while (file_exists("/aaa/file.txt")) {
      sleep(5);
    }
//set display property of the invisibleLink class to block and continue

问题是我不知道该怎么做最后一步,我见过的用PHP修改CSS的每个线程都不适用于我的用例。

The thing is I don't know how to do this last step and every thread I've seen about modifying CSS with PHP doesn't work with my use case.

推荐答案

PHP在屏幕上没有任何内容显示之前就已执行,因此您可能无法做到这一点:代码将只休眠5个小时,然后继续生成其余的代码。在向用户显示之前是html。

PHP executes before anything is displayed on the screen, so you are probably not going to be able to do that: the code would simply sleep for 5 and then continue with generating the rest of the html before displaying to the user.

您可能想要做的是将按钮标记为display:无,然后在页面加载完成后具有一个js函数,调用一个php页面,该页面返回文件是否存在。让js函数循环直到php页面说文件消失了,然后让js函数显示按钮并停止循环。

What you might want to do instead is mark the button as display: none and then when the page is done loading have a js function that calls a php page that returns whether the file exists or not. Have the js function loop until the php page says the file is gone, then have the js function display the button and stop looping.

<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") { // or whatever you want the response to be
                        $('#test_btn').show();
                    }
                    else {
                        checkFile(); // you can add a setTimeout if you don't want this running too often
                    }
                }
            });
        }
    }
</script>

然后,您的文件检查器php可能类似于您的文件:

Then your file checker php can be something similar to what you had:

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

这篇关于服务器上文件存在时不显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆