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!
<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>