php - How can I overwrite a file, when the file I want to upload already exists? -
i want overwrite file, if exists in folder. here 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 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 overwritten";
update: did mistake in question. changed it. on check.php there should statement "file exists" , button directs overwrite.php , overwrite file. (no second input field)
when creating form again need specify enctype="multipart/form-data"
echo "<form action='overwrite.php' method='post' enctype='multipart/form-data'> <input type='file' name='file'> <button type='submit'> overwrite</button> </form>";
without enctype="multipart/form-data"
it wont let perform file upload operation
other name should not value should static can use
Comments
Post a Comment