Javascript 使用AJAX检查(下载)文件是否存在&;PHP
我有一个下载页面,访问者可以在输入字段中输入ZIP文件的名称(不带扩展名) index.php:Javascript 使用AJAX检查(下载)文件是否存在&;PHP,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个下载页面,访问者可以在输入字段中输入ZIP文件的名称(不带扩展名) index.php: <form action="download-script.php" method="post"> <input type="text" name="file" placeholder="Enter filename here" /> <input type="submit" value="Download starten" /> </for
<form action="download-script.php" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" value="Download starten" />
</form>
<?php
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else {
echo "File not found";
exit;
}
?>
ZIP文件存储在单独的文件夹“files”中。如果访问者知道文件的名称,一切都很好。如果文件名拼写错误或为空,脚本将显示一条错误消息:
download-script.php:
<form action="download-script.php" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" value="Download starten" />
</form>
<?php
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else {
echo "File not found";
exit;
}
?>
我的目标是在
index.php
上显示错误消息,而不是在下载脚本.php
上显示错误消息,因为下载脚本.php
只会显示错误消息。您可以这样做,如果有帮助,请告诉我
index.php
<?php
if (isset($_POST['submit_form'])) {
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file_path = 'download/'. $file . '.zip';
if (file_exists($file_path)) {
echo "<script>window.location.href='download-script.php?file=".$file."'</script>";
}
else { echo "File not found"; exit; }
}
?>
<form action="" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" name="submit_form" value="Download starten" />
</form>
<?php
$file = 'download/'. $_GET['file'] . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
<?php
if (isset($_POST['submit_form'])) {
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file_path = 'download/'. $file . '.zip';
if (file_exists($file_path)) {
header('Content-Disposition: attachement; filename=' . basename($file_path));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else { echo "File not found"; exit; }
}
?>
<form action="" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" name="submit_form" value="Download starten" />
</form>
<?php
if (isset($_REQUEST["error"])) {
echo "File not found";
}
?>
<form action="download-script.php" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" value="Download starten" />
</form>
<?php
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else {
header('Location: index.php?error=1');
exit;
}
?>
在index.php中
<?php
if (isset($_POST['submit_form'])) {
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file_path = 'download/'. $file . '.zip';
if (file_exists($file_path)) {
echo "<script>window.location.href='download-script.php?file=".$file."'</script>";
}
else { echo "File not found"; exit; }
}
?>
<form action="" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" name="submit_form" value="Download starten" />
</form>
<?php
$file = 'download/'. $_GET['file'] . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
<?php
if (isset($_POST['submit_form'])) {
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file_path = 'download/'. $file . '.zip';
if (file_exists($file_path)) {
header('Content-Disposition: attachement; filename=' . basename($file_path));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else { echo "File not found"; exit; }
}
?>
<form action="" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" name="submit_form" value="Download starten" />
</form>
<?php
if (isset($_REQUEST["error"])) {
echo "File not found";
}
?>
<form action="download-script.php" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" value="Download starten" />
</form>
<?php
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else {
header('Location: index.php?error=1');
exit;
}
?>
在下载script.php
<?php
if (isset($_POST['submit_form'])) {
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file_path = 'download/'. $file . '.zip';
if (file_exists($file_path)) {
echo "<script>window.location.href='download-script.php?file=".$file."'</script>";
}
else { echo "File not found"; exit; }
}
?>
<form action="" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" name="submit_form" value="Download starten" />
</form>
<?php
$file = 'download/'. $_GET['file'] . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
<?php
if (isset($_POST['submit_form'])) {
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file_path = 'download/'. $file . '.zip';
if (file_exists($file_path)) {
header('Content-Disposition: attachement; filename=' . basename($file_path));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else { echo "File not found"; exit; }
}
?>
<form action="" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" name="submit_form" value="Download starten" />
</form>
<?php
if (isset($_REQUEST["error"])) {
echo "File not found";
}
?>
<form action="download-script.php" method="post">
<input type="text" name="file" placeholder="Enter filename here" />
<input type="submit" value="Download starten" />
</form>
<?php
$file = preg_replace("/[^0-9a-z.\-_ ]/i", "", $_POST['file']);
$file = 'download/' . $file . '.zip';
if (file_exists($file)) {
header('Content-Disposition: attachement; filename=' . basename($file));
header('Content-Type: application/force-download');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Connection: close');
readfile($file);
}
else {
header('Location: index.php?error=1');
exit;
}
?>
您可以尝试ajax,但它更复杂,更难调试,请参见
或者举个例子。嗨,施里坎特·马夫兰卡!谢谢你的支持!错误消息工作正常!但是下载脚本不再工作了。JavaScript
window.location.href='download-script.php?file=“.$file.”有问题。
。脚本通常在后台工作。现在javascript将加载/显示download-script.php,而不是加载文件。此链接出现在浏览器中:…/download script.php?file=download/testfile.zip,不会加载任何文件…我的答案中做了一些更改,请检查并让我知道:)嗨,Shrikant!我仍然有相同的问题:window.location.href='download-script.php?file=“.$file.”
将加载/显示脚本(空白页),但不加载文件:-(修改了download script.php
;添加了缺少的.zip
扩展名。请现在检查,它应该可以工作了。非常好,谢谢!但现在我们遇到了一个新问题:按下下载按钮后,表单(表单,页脚)消失后开始的页面内容。这是生成的源代码:[…]window.location.href='download-script.php?file=test'
您好,谢谢您的脚本!您的解决方案运行良好:-)