Web EndMemo

HTML File Upload


HTML can upload files if using such as PHP handling at the server side.

Following is a file upload example. There is a 10kb limit for uploaded file, please use a small sized file to try. You can change the file size limit in your own code.

Upload File:



Following is the HTML code:
<FORM name=mem action="<?PHP $_SERVER['PHP_SELF'] ?>" 
method="post" enctype="multipart/form-data">
<TABLE cellPadding=4 width=590 bgColor=#ffffff><TBODY>
<TR>
  <TD width=100>Upload File:</TD>
  <TD width=350>
		<input name="uploadedfile" type="file" value="Browse" 
		style="font-size:16px;width:350px">
  </TD>
</TR>
<TR>
  <TD></TD>
  <TD><input type="submit" name="submit" value="Upload" /></TD>
</TR>
</TBODY></TABLE>
</FORM>
<br>
<?PHP
   if ($str != "") echo "$str\n";
?>


The PHP code handling the HTML file upload:
<?PHP
$str = "";
if (isset($_POST['submit']))
{  
$uploaddir = "tp/";
$filetype=array("jpg","gif","bmp","jpeg","png","pdf","xps","txt");
$origfile = basename( $_FILES['uploadedfile']['name']); 
	
$a = strtolower(substr(strrchr($_FILES['uploadedfile']['name'], '.'), 1));
$sz = $_FILES['uploadedfile']['size'];

if ($sz/1024 > 10) 
{
  $str = "Uploaded file size can not be larger than 20k, upload failed.";
}	
else if(!in_array($a,$filetype))
{
  $text=implode(",",$type);
  $str = "Only following file types are allowed to upload: $text";
}
else
{
  $uploadfile = $uploaddir . $_FILES['uploadedfile']['name'];	
  $b = $_FILES['uploadedfile']['tmp_name'];
  if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$uploadfile))
  {
	$str = "Download $origfile";
  }
}
}
?>