Follow me on Twitter to receive updates, free scripts and ongoing announcements!

Uploading doc/docx file to my server

Written by Codes Tips on March 19, 2009 – 6:09 am -

 

You can do allowing of doc/docx file in 2 modes: in html source with the property accept or by php code.

For uploading a doc/docx file you should first have a form where youhave all fields you need to send to server and another field that is of type file.

File:
<form enctype="multipart/form-data" method="POST">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>

 

We should specify the enctype of the form to multipart/form-data in order to upload a file.

 

Here is a brief explanation on how we can write the php code to upload one file to our server:

First we should set and verify the file extensions that we will allow the user to upload. We will allow only files with doc/docx extension. We should also verify that the filesize it’s greater then 10 bytes to be sure that is a doc/dox file. If we have no errors then we can use move_uploaded_file function to upload our doc/docx file.

Php code to upload doc/docx file to the server

 

//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 = '/home/uername/folderroot/';
 
  $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>';
 
<span> </span> //verify if the file size of the file being uploaded is greater then 1
 
   if(filesize($_FILES['filename']['tmp_name']) &lt; $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;
 
}

Tags: , ,
Posted in PHP/MYSQL |

Leave a Comment

You must be logged in to post a comment.


Plugintaylor.com - Plugintaylor and Plugin Directory