【发布时间】:2019-06-06 10:14:01
【问题描述】:
当检查文件是否已存在时,我正在尝试在提交后上传和覆盖文件。 因此,如果首先检查文件夹中是否已存在文件,然后我会给出选项:“你想覆盖它吗?”如果是,则应上传文件并覆盖旧文件。
这是我目前所拥有的:
...
// file is ready to be uploaded
$tmpFilePath = $_FILES['file']['tmp_name'];
$newFilePath = $dir.'/' . $_FILES['file']['name'];
// check if file already exists
if(file_exists($dir.'/' . $_FILES['file']['name'])) {
include('includes/file_exists.php');
exit;
}
//finally upload the file
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
include('includes/success.php'); // echo
exit;
}
file_exists.php 是这个代码:
<span>File already exists! Do you want to overwrite it?</span>
<form class="sfmform" action="" method="post">
<input type="hidden" name="overwrite" value="<?php echo $_FILES['file']['tmp_name']; ?>" />
<input type="submit" class=" btn btn-primary" name="submitoverwrite" value="Yes"/>
</form>
(所有请求都由 AJAX 处理)
【问题讨论】:
-
为什么是
exit()? -
我动态地重新加载页面;否则有些东西会输出两次
-
这仍然不是退出的充分理由。你不应该杀死脚本,因为你不知道如何控制它。
标签: php file-upload overwrite