服务器上存在文件时不显示按钮
Don't display a button while file exists on server
我试图仅在网络服务器上的特定文件被删除时才在 HTML 页面上显示下载按钮。我想我会使用 CSS display: none;
然后 PHP 带有 while 循环的脚本,看起来像这样:
while (file_exists("/aaa/file.txt")) {
sleep(5);
}
//set display property of the invisibleLink class to block and continue
问题是我不知道如何执行最后一步,而且我看到的关于用 PHP 修改 CSS 的每个线程都不适用于我的用例。
只需构建按钮并使用 class 隐藏它,如下所示:
<style>
.hidden{ display:none;}
</style>
<?php
if(!file_exists("path") ){ $class = "hidden" }
echo "<input type='button' class='$class' name='stuff'>woo</button>";
?>
PHP 在屏幕上显示任何内容之前执行,因此您可能无法做到这一点:代码将简单地休眠 5,然后继续生成其余的 html 在显示给用户之前。
您可能想要做的是将按钮标记为显示:none 然后当页面加载完成时有一个 js 函数调用 php 页面 returns 文件是否存在。让 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 可能与您拥有的类似:
if (file_exists("/aaa/file.txt")) {
echo "exists";
}
else {
echo "deleted";
}
我试图仅在网络服务器上的特定文件被删除时才在 HTML 页面上显示下载按钮。我想我会使用 CSS display: none;
然后 PHP 带有 while 循环的脚本,看起来像这样:
while (file_exists("/aaa/file.txt")) {
sleep(5);
}
//set display property of the invisibleLink class to block and continue
问题是我不知道如何执行最后一步,而且我看到的关于用 PHP 修改 CSS 的每个线程都不适用于我的用例。
只需构建按钮并使用 class 隐藏它,如下所示:
<style>
.hidden{ display:none;}
</style>
<?php
if(!file_exists("path") ){ $class = "hidden" }
echo "<input type='button' class='$class' name='stuff'>woo</button>";
?>
PHP 在屏幕上显示任何内容之前执行,因此您可能无法做到这一点:代码将简单地休眠 5,然后继续生成其余的 html 在显示给用户之前。
您可能想要做的是将按钮标记为显示:none 然后当页面加载完成时有一个 js 函数调用 php 页面 returns 文件是否存在。让 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 可能与您拥有的类似:
if (file_exists("/aaa/file.txt")) {
echo "exists";
}
else {
echo "deleted";
}