Oct 19, 2012

Upload .doc and .docx file using PHP


To download code - Click Here



Step 1



Create one html file named index.html and paste the following code into it.



<html>



<body>



<form enctype="multipart/form-data" method="POST" action="upload.php">This is the code for html:



<table border="0">



<tbody>



<tr>



<td align="left">File:</td>



<td><input accept="doc/docx" name="filename" size="40" type="file" /></td>



</tr>



<tr>



<td><input name="Upload" type="submit" value="Upload" /></td>



</tr>



</tbody></table>



</form>



</body>



</html>






Step 2



Create one PHP file named upload.php and paste the following code.



<?php



//if we clicked on Upload button



if($_POST['Upload'] == 'Upload')



  {



  //make the allowed extensions



  $goodExtensions = array(



  '.doc',



  '.docx',



  ); 



  $error='';



  //set the current directory where you wanna upload the doc/docx files



  $uploaddir = './ ';



  $name = $_FILES['filename']['name'];//get the name of the file that will be uploaded



  $min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes)



  $stem=substr($name,0,strpos($name,'.'));



  //take the file extension



  $extension = substr($name, strpos($name,'.'), strlen($name)-1);



  //verify if the file extension is doc or docx



   if(!in_array($extension,$goodExtensions))



     $error.='Extension not allowed<br>';



echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 1



   if(filesize($_FILES['filename']['tmp_name']) < $min_filesize)



     $error.='File size too small<br>'."\n";



  $uploadfile = $uploaddir . $stem.$extension;



$filename=$stem.$extension;



if ($error=='')



{



//upload the file to



if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) {



echo 'File Uploaded. Thank You.';



}



}



else echo $error;



}



?>





Step 3



Copy both index.html and upload.php into WAMP folder. Open index.html and browse one .doc or .docx file and click upload button



To download code - Click Here



No comments:

Post a Comment