I wrote a simple PHP script which allows the user to upload a file to the server. It works, but I'm not very sure about security. Could you give me some hints about security mistakes?
In a nutshell: The user uploads a file from form.html, then a file is saved in uploads/ directory on the server, and a direct link to it is recorded in a database. Then that link is presented to the user, so he/she is able to download the file. The execution of that file is forbidden using .htaccess.
form.html
<div id="quickFileSubmit" style="margin-left: 20px; display: none">
<table>
<tr>
<td>
<form enctype="multipart/form-data" class="smallForm"
action="action.php?act=quickFileSubmit" method="post"
style="margin-top: 5px;">
<label style="font-size: 12px;">File:</label>
<input type="file" name="uploadfile" readonly="true" size="30"/>
<input type="submit" value="Submit" />
</form>
</td>
<td style="width: 100%;"></td>
</tr>
</table>
</div>
action.php
//some actions
else if ($_REQUEST["act"] == "quickFileSubmit") {
$basename = getUploadBasename();
uploadFile();
header("location:index.php");
}
function uploadFile() {
if (strlen($_FILES['uploadfile']["name"]) > 0) {
$target_path = "uploads/";
$basename = basename( $_FILES['uploadfile']['name']);
$target_path = $target_path . $basename;
if (encode($basename) == $basename) {
if(move_uploaded_file($_FILES['uploadfile']['tmp_name'], $target_path)) {
// here goes saving of file info
return $basename;
} else{
// upload failed.
echo "File upload failed.";
return null;
}
} else {
echo "Illegal characters in file name.";
return null;
}
}
}
Also, I have put .htaccess in the uploads dir:
.htaccess
SetHandler default-handler
Options -Indexes