I was writing this code to upload files from my computer to a server. I finished the script and it worked fine, but then I wanted to make sure it didn't overwrite anything.
For this, I used "if(file_exists(...))" as shown in my code. It doesn't work, however, and you can still overwrite things.
Can someone say where the problem is or what I should do? Thanks.
The code:
<html>
<head>
<title>Upload a file to our servers...</title>
</head>
<body>
<?php
print "Upload a file to the server: \n";
print " <form method=\"post\" action=\"simpleupload.php\" enctype=\"multipart/form-data\"> \n";
print "File <input type=\"file\" name=\"File\" size=30><br> \n";
print "<input type=\"submit\" value=\"Submit\"> \n";
print "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"2048\">\n";
print "</form> \n\n";
if($File) // if file exists
{
print "Details:<p>\n";
print "File Name: $File_name<br>\n";
print "File size: $File_size<p>\n";
if(file_exists(articles/$File_name)) // if file name already exists
{
print "File name in use. Please choose a different name";
}
else
{
if (copy($File, "articles/$File_name"))
{
print "Your file uploaded successfully! <p>\n";
}
else
{
print "Was unable to upload file <p>\n";
}
}
unlink ($File); // deletes file
}
?>
</body>
</html>