文件上传和覆盖php中相同名称的文件

  • 本文关键字:文件 php 覆盖 php html
  • 更新时间 :
  • 英文 :


我制作了一个应用程序,允许用户上传目录中的文件。如果用户想要更改文件(上传的文件是错误的文档(,用户可以用新文件覆盖它。例如,用户上传了带有错误文件的身份证,然后用户可以通过单击按钮更改带有正确文件的文件。

代码:

HTML

<form  method="post">
<input class="form-control white_bg" id="allfile" name="allFile"  type="file" required><br>
<button type="submit" name="save" class="btn btn-info btn-min-width mr-1 mb-1"">Save</button>
</form>

PHP

$file = $_FILES["allfile"]["tmp_name"];
$name = $fileName;
$extensionPhoto = substr($photo,strlen($photo)-4,strlen($photo));
$allowed_extensions = array(".jpg","jpeg",".png",".gif",".pdf",".PDF");
$file1 = $fileName;
move_uploaded_file($_FILES[$file],"userdocs/".$file1);

检查文件是否已经存在。删除它并移动新文件

if(file_exists("userdocs/".$file1)) {
unlink("userdocs/".$file1);
move_uploaded_file($_FILES[$file],"userdocs/".$file1);
}

最新更新