I wrote a PHP script to upload a .txt file and it works partially:
<?php
$allowedExts = array("txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$dest = "/etc/squid/squid.conf";
if ((($_FILES["file"]["type"] == "text/plain")
&& ($_FILES["file"]["size"] < 170000)
&& in_array($extension, $allowedExts)))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "<div style='text-align: center'>";
echo "Upload File Success!!<br><br>";
echo "FIle : " . $_FILES["file"]["name"] . "<br>";
echo "Type : " . $_FILES["file"]["type"] . "<br>";
echo "Size : " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "</div>";
move_uploaded_file($_FILES["file"]["tmp_name"],
"/var/www/squid_proxy/upload/" . $_FILES["file"]["name"]);
copy("/var/www/squid_proxy/upload/" . $_FILES["file"]["name"],
$dest);
}
}
else
{
echo "<div style='text-align: center'>";
echo "Error, File upload only .txt";
echo "</div>";
}
?>
Both file type and MIME validation work well, but I want a validation of the file's content. The file is stuctured with headers. See this example:
#ADVANCED
#WIZARD
Before the file upload to server, it must be checked that the file contains #ADVANCED in the first line and #WIZARD in the third line.
If the headers are missing or the file is empty, an error message should be shown.
How can I validate the content of the uploaded file?