File Upload

File Upload in PHP

FileUpload

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>

<body>
<form enctype=”multipart/form-data” action=”upload.php” method=”POST”>
Please choose a file: <input name=”uploaded” type=”file” />&nbsp;&nbsp;&nbsp;<input type=”reset” value=”reset” /><br />
<input type=”submit” value=”Upload” />
</form>
</body>
</html>

upload.php

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>

<body>
<b>
<?php
$target = “upload/”;
$target = $target . basename( $_FILES[‘uploaded’][‘name’]) ;
$ok=1;

$uploaded_size=$_FILES[‘uploaded’][‘size’];
echo “File Size: $uploaded_size<br>”;
$uploaded_type=$_FILES[‘uploaded’][‘type’];
echo “File Type: $uploaded_type<br>”;
//This is our size condition
if ($uploaded_size > 350000)
{
echo “Your file is too large.<br>”;
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type !=”application/msword” && $uploaded_type !=”application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”)
{
echo “Only .doc or .xls files are allowed to upload<br>”;
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo “Sorry your file was not uploaded”;
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES[‘uploaded’][‘tmp_name’], $target))
{
echo “The file “. basename( $_FILES[‘uploaded’][‘name’]). ” has been uploaded Successfully….”;
}
else
{
echo “Sorry, there was a problem uploading your file.”;
}
}

?>
</b>
</body>
</html>

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.