This form uploads a .txt file to the directory /upload_text/
It does this by checking for the file extention.
If it is true then the file is submitted. If false a message is displayed.
<?php
define ("MAX_SIZE","100");function getExtension($str){$i = strrpos($str,".");
if (!$i) {return "";}$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;}
$errors=0;
if(isset($_POST['Submit'])){$text=$_FILES['text']['name'];
if ($text){$filename = stripslashes($_FILES['text']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "txt")){echo '<h3>Error</h3><p>Upload atempt is not an text file.</p>';
$errors=1;}
else{$size=filesize($_FILES['text']['tmp_name']);
if ($size > MAX_SIZE*1024){echo '<h3>Error</h3><p>File text size is too big</p>';
$errors=1;}
$image_name=$filename;
$newname="./upload_text/".$image_name;
$copied = copy($_FILES['text']['tmp_name'], $newname);
if (!$copied){echo '<h3>Error</h3><p>Upload unsuccessfull.</p>';
$errors=1;}
}
}
}
if(isset($_POST['Submit']) && !$errors){echo "<h3>Success</h3><p>Text uploaded successfully.</p>";}
?>
//Start PHP protocol
//Define maximum size in Kb
//Function to start file extention check
//Variable equals find string
//If variable does not equals find string return blank
//Variable equals string minus first varibale
//Variable equals subtract string from second variable
//Return the extention
//Variable error equals 0
//If submit button pressed
//Variable equals files text and name
//If text equals
//Variable name with no slashes
//Varibable equals extension of the file...
//...in a lowercase format
//If file is not txt
//Print message
//Add error to counter
//Otherwise
//Get file size
//If file size is bigger than 1024Kb
//Print message
//Add error to counter
//Set filename as text name
//Define upload directory
//If anything fails print error message
//If not copied
//Print message
//Add error to
//Close else
//Close if ($text)
//Close submit
//If submit and no errors
//Print message
//Close PHP protocol