I want to overwrite a file, if it already exists in the folder. Here is my code:
index.php
<form action="check.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" value="upload">Upload</button>
</form>
check.php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
$target_file = 'files/'.basename($_FILES["file"]["name"]);
if (file_exists($target_file)) {
echo "File already exist";
echo "<form action='overwrite.php' method='post'>
<button type='submit'> Overwrite</button>
</form>";
}
}
overwrite.php
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
echo "The file is overwritten";
Update: I did a mistake in my question. Now I changed it. On check.php there should only be the statement "File already exists" and a button which directs to the overwrite.php and overwrite the file. (No second input field)