显示和隐藏按钮 (href=) 如果文件存在于服务器中
Show and Hide Button (href=) if file exist in server or not
我尝试编辑一个类似的解决方案,但它没有达到预期效果。我不确定我的编码可能是错误的,我使用的是 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";
}
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
将 CSS class 添加到您的按钮并在您的页面中包含 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);
}
另一种方法是向用户提供现有文件的列表,并允许他们从列表中select。
<?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>
我尝试编辑一个类似的解决方案,但它没有达到预期效果。我不确定我的编码可能是错误的,我使用的是 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";
}
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
将 CSS class 添加到您的按钮并在您的页面中包含 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);
}
另一种方法是向用户提供现有文件的列表,并允许他们从列表中select。
<?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>