用 php 下载.js文件



我得到了以下文件:

索引.php:

<html>
<head>
<title>Admin Panel</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br /><br />
<div class="container">
<h2 align="center">Admin Panel</a></h2>
<br />
<div align="right">
<a href="download_current.php"><button  type="button" name="create_folder" id="create_folder" class="btn btn-success">Get current file</button></a>
</div>
<br />
<div class="table-responsive" id="folder_table">
</div>
</div>
</body>
</html>
<div id="uploadModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Upload File</h4>
</div>
<div class="modal-body">
<form method="post" id="upload_form" enctype='multipart/form-data' action="upfile.php">
<p>Select Js File
<input type="file" name="upload_file" accept=".js"/></p>
<br />
<input type="hidden" name="hidden_folder_name" id="hidden_folder_name" />
<input type="submit" name="upload_button" class="btn btn-info" value="Upload" />
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<div id="filelistModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times;</button>
<h4 class="modal-title">File List</h4>
</div>
<div class="modal-body" id="file_list">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
load_folder_list();
function load_folder_list() {
var action = "fetch";
$.ajax({
url: "action.php",
method: "POST",
data: {
action: action
},
success: function(data) {
$('#folder_table').html(data);
}
});
}
$(document).on('click', '.upload', function() {
var folder_name = $(this).data("name");
$('#hidden_folder_name').val(folder_name);
$('#uploadModal').modal('show');
});
$('#upload_form').on('submit', function() {
$.ajax({
url: "upfile.php",
method: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
});
});

$(document).on('click', '.view_files', function() {
var folder_name = $(this).data("name");
var action = "fetch_files";
$.ajax({
url: "action.php",
method: "POST",
data: {
action: action,
folder_name: folder_name
},
success: function(data) {
$('#file_list').html(data);
$('#filelistModal').modal('show');
}
});
});
$(document).on('click', '.remove_file', function() {
var path = $(this).attr("id");
var action = "remove_file";
$.ajax({
url: "action.php",
method: "POST",
data: {
path: path,
action: action
},
success: function(data) {
$('#filelistModal').modal('hide');
load_folder_list();
}
});
});
});
</script>

操作.php:

<?php
function format_folder_size($size)
{
if ($size >= 1073741824) {
$size = number_format($size / 1073741824, 2) . ' GB';
} elseif ($size >= 1048576) {
$size = number_format($size / 1048576, 2) . ' MB';
} elseif ($size >= 1024) {
$size = number_format($size / 1024, 2) . ' KB';
} elseif ($size > 1) {
$size = $size . ' bytes';
} elseif ($size == 1) {
$size = $size . ' byte';
} else {
$size = '0 bytes';
}
return $size;
}
function get_folder_size($folder_name)
{
$total_size = 0;
$file_data = scandir($folder_name);
foreach ($file_data as $file) {
if ($file === '.' or $file === '..') {
continue;
} else {
$path = $folder_name . '/' . $file;
$total_size = $total_size + filesize($path);
}
}
return format_folder_size($total_size);
}
if (isset($_POST["action"])) {
if ($_POST["action"] == "fetch") {
$folder = array_filter(glob('*'), 'is_dir');
$output = '
<table class="table table-bordered table-striped">
<tr>
<th>Folder Name</th>
<th>Total File</th>
<th>Size</th>
<th>Upload File</th>
<th>View Uploaded File</th>
</tr>
';
if (count($folder) > 0) {
foreach ($folder as $name) {
$output .= '
<tr>
<td>' . $name . '</td>
<td>' . (count(scandir($name)) - 2) . '</td>
<td>' . get_folder_size($name) . '</td>
<td><button type="button" name="upload" data-name="' . $name . '" class="upload btn btn-info btn-xs">Upload File</button></td>
<td><button type="button" name="view_files" data-name="' . $name . '" class="view_files btn btn-default btn-xs">View Files</button></td>
</tr>';
}
} else {
$output .= '
<tr>
<td colspan="6">No Folder Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
}

if ($_POST["action"] == "fetch_files") {
$file_data = scandir($_POST["folder_name"]);
$output = '
<table class="table table-bordered table-striped">
<tr>
<th>File Name</th>
<th>Download</th>
</tr>
';
foreach ($file_data as $file) {
if ($file === '.' or $file === '..') {
continue;
} else {
$script = 'download.php';
$downloadlink = $script . '/' . $_POST["folder_name"] . '/' . $file;
$path = $_POST["folder_name"] . '/' . $file;
$output .= '
<tr>
<td contenteditable="false" data-folder_name="' . $_POST["folder_name"] . '"  data-file_name = "' . $file . '" class="change_file_name">' . $file . '</td>
<td><a href="' . $downloadlink . '"><button name="remove_file" class="remove_file btn btn-danger btn-xs" id="' . $path . '">Get it</button></td></a>
</tr>
';
}
}
$output .= '</table>';
echo $output;
}
function test()
{
if ($_POST["action"] == "remove_file") {
if (file_exists($_POST["path"])) {
readfile($_POST["path"]);
}
}
}
}

下载.php

<?php
//file path in server
$path = $_POST["folder_name"] . '/' . $file;
// check if file exist in server
if (file_exists($path)) {
header("Cache-Control: public");
header('Content-Description: File Transfer');
header('Content-Type: application/x-javascript');
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Length: ' . filesize($path));
// Clear output buffer
flush();
readfile($path);
exit();
} else {
echo "File not found.";
}
?>

现在的问题是,当我从服务器下载javascript文件时,它会下载文件,但其中没有任何内容。在服务器上,文件包含内容,因此从网站下载文件时一定有问题。我认为问题出在下载上.php但在我看来语法是正确的。这也可能是索引.php和行动.php中remove_file功能的问题。参见 $(document(.on('click', '.remove_file', function(( in index.php

我会说你需要html。我不知道你是否可以为此使用 PHP。如果你真的需要使用 PHP:

<?php
echo "<a href='file.js' download>Click Here to Download</a>";
?>

从本质上讲,这样做是创建一个链接,用户在其中单击以下载文件。

最新更新