我正在尝试仅当web服务器上的特定文件被删除时才在HTML页面上显示下载按钮。我想我应该使用一个CSS display: none;,然后是一个带有while循环的PHP脚本,如下所示:
while (file_exists("/aaa/file.txt")) {
sleep(5);
}
//set display property of the invisibleLink class to block and continue问题是,我不知道如何完成最后一步,并且我看到的关于用PHP修改CSS的每个线程都不适用于我的用例。
发布于 2017-08-29 22:22:26
PHP在屏幕上显示任何内容之前执行,所以您可能无法做到这一点:代码将简单地休眠5分钟,然后继续生成html的其余部分,然后显示给用户。
相反,您可能想要做的是将按钮标记为display: none,然后在页面加载完成后,让一个js函数调用一个php页面,该页面返回文件是否存在。让js函数循环,直到php页面显示文件消失,然后让js函数显示按钮并停止循环。
<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就可以和你的文件检查器php类似:
if (file_exists("/aaa/file.txt")) {
echo "exists";
}
else {
echo "deleted";
}发布于 2017-08-29 22:11:42
只需构建按钮并使用如下所示的类隐藏它:
<style>
.hidden{ display:none;}
</style>
<?php
if(!file_exists("path") ){ $class = "hidden" }
echo "<input type='button' class='$class' name='stuff'>woo</button>";
?>https://stackoverflow.com/questions/45941277
复制相似问题