不必创建新文件,而是必须删除先前提交的文件并用新文件替换。
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="attachment" id="attachment" onchange="document.getElementById('moreUploadsLink').style.display = 'block';" />
<input type="submit" value="Submit" name="submit">
</form>
upload.php的
<?php
$target_dir = "uploads/";
$target_file = $target_dir . date('d_m_Y_H_i_s') . '_'. $_FILES["attachment"]["name"];
$uploadOk = 1;
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Move the file
move_uploaded_file($_FILES["attachment"]["tmp_name"], $target_file);
?>
答案 0 :(得分:0)
PHP上传然后写文件:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . date('d_m_Y_H_i_s') . '_'.$_FILES["attachment"]["name"];
$uploadOk = 1;
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
// Move the file
move_uploaded_file($_FILES["attachment"]["tmp_name"], $target_file);
$file = fopen($target_file,"w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
答案 1 :(得分:0)
你基本上需要做两件事:
每当用户上传新文件时,请将其存储为他们上传的最后一个文件。会话状态似乎是一个合理的地方来跟踪这一点,至少目前是这样:
$_SESSION['lastFile'] = $target_file;
然后,每当用户上传另一个文件时,也要在删除之前删除最后一个文件:
if (isset($_SESSION['lastFile'])) {
// This would be a good place to validate the file path first.
// Make sure it exists, authorize the user before deleting it, etc.
unlink($_SESSION['lastFile']);
}
如果要跟踪会话状态范围之外的“上一个文件”(例如,重新启动Web服务器),那么您需要跟踪数据库记录之类的“先前文件”而是为那个用户。