php - 如果文件存在于服务器中,则显示和隐藏按钮 (href=)
问题描述
我尝试编辑一个类似的解决方案,但它没有达到预期的效果。我不确定我的编码可能是错误的并且我使用的是 html。如果文件存在,有没有办法隐藏按钮(href =)然后显示它是否找不到?谢谢你!
参考:链接
<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
// 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>
推荐阅读
- bash - 在 osx 上使用 xld 在命令行中拆分 cued 音频文件
- python-3.x - Numpy 数组列表未成功附加所有整数
- ssl - 如何使用从1&1 IONOS上传的private_key.key / ssl_certificate.cer / ssl_certificate_INTERMEDIATE.cer 并配置tomcat服务器
- getsockopt - 如何 printf() 声明和初始化 getsockname() 函数?
- controller - 如何使用 react-hook-forms 切换单选按钮选择?
- python - AttributeError:类型对象“数据库”没有属性“scoreDB”
- python - 如何用空字典初始化类 - Python
- ubuntu - 我无法确认任何 api 正在通过 microk8s 中的 localhost 工作
- snowflake-cloud-data-platform - Snowflake - 删除 Schema 中的所有外键,可能使用 CTE 删除
- django - 如何在 VSCode 终端中激活 python 虚拟环境