# Uploaded file can overwrite old file, how?

**URL:** https://forum.kirupa.com/t/uploaded-file-can-overwrite-old-file-how/59399
**Category:** Uncategorized
**Created:** [July 30, 2004, 11:20pm UTC](https://forum.kirupa.com/t/uploaded-file-can-overwrite-old-file-how/59399 "2004-07-30T23:20:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![midnite](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@midnite](https://forum.kirupa.com/u/midnite)
#### Post date: [July 30, 2004, 11:20pm UTC](https://forum.kirupa.com/t/uploaded-file-can-overwrite-old-file-how/59399/1 "2004-07-30T23:20:00Z")

</div>

hi there, php nOOb here. :stunned:

i’m using this php code to upload files to my server. it works fine except that if i try to upload a file with the same name as one that’s already been uploaded, it will return an error instead of replacing it. how can i script it so that it will replace it? many many thanks!

```auto
<html> 
<head> 
<title>File Uploader</title> 
</head> 
<body bgcolor="silver"> 
<?php 
ini_set("display_errors", 1);
error_reporting(E_ALL & ~E_NOTICE);
$file = $_FILES['file'];
$file_name = $_FILES['file']['name'];
$file_size = $_FILES['file']['size'];
$file_temp = $_FILES['file']['tmp_name'];
$bad_files = "0";
$max_size = "5242880"; // Maximum File Size (In Bytes) 
$extensions = ".gif|.txt|.htm|.html|.swf|.jpg"; // Allowed File Extensions 
$file_name = stripslashes ($file_name); 
if ($file) { 
if ($file_size > $bad_files) {
if ($file_size <= $max_size) { 
if (ereg($extensions , $file_name)) { 
print "File name: $file_name<p>
"; 
print "File size: $file_size<p>
"; 
if (move_uploaded_file($file_temp, "files_uploaded/$file_name")) { 
print "<font color=\"blue\" font size=\"3\">Your file was successfully uploaded !</font><p>
"; 
}else{ 
print "<font color=\"red\" font size=\"3\">Your File could not be uploaded!</font></P>
"; 
} 
}else{ 
print "<font color=\"red\" font size=\"3\">Bad File Extension (gif, jpg, txt, htm, html, swf) Only!</font><p>
"; 
} 
}else{ 
print "<font color=\"red\" font size=\"3\">The File Size is Too Big!</font><p>
"; 
}
}else{
print "<font color=\"red\" font size=\"3\">File Must Be Greater Than 0 Byte's</font><p>
";
}
} 
print "Upload a File to the server: 
<br><form action='index.php' method='post' enctype='multipart/form-data'> 
<input type='file' name='file'><br><input type='submit' value='Upload'>
<br><hr>Files Allowed: gif, jpg, txt, htm, html, swf <br> Max File Size: 5 MB</form>"; 
?> 
</body> 
</html> 

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 31, 2004, 5:42am UTC](https://forum.kirupa.com/t/uploaded-file-can-overwrite-old-file-how/59399/2 "2004-07-31T05:42:25Z")

</div>

unlink it first 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [July 31, 2004, 6:05am UTC](https://forum.kirupa.com/t/uploaded-file-can-overwrite-old-file-how/59399/3 "2004-07-31T06:05:19Z")

</div>

i’m even more of a n00b than i thought! how do i unlink it :h:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [August 1, 2004, 1:50am UTC](https://forum.kirupa.com/t/uploaded-file-can-overwrite-old-file-how/59399/4 "2004-08-01T01:50:59Z")

</div>

Unlink()
