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 | No Comments »

Read bigger and small text file delphi

Written by Codes Tips on March 18, 2009 – 10:08 am -

Read small text files in Delphi

First we should declare a variable of type TextFile like this

var f:textfile;//defines variable for type TextFile for maintaining data from a file.

We will need also other variables to read the file:

var s:string;
ch:char;

If you wanna read contents of a file in Delphi you should first assign to a variable the name of the file you wanna read. You can use the following statement:

assignfile(f,path+'filename.txt');// where path can be the path to the directory

If the file you wanna read it’s inside the folder project then you can use the following code:

path:= ExtractFilePath(application.exename);//ExtractFilePath it's a function that returns a string with the path of an .exe

Next if you should let the compiler know that you wanna read the file.

reset(f);

Now you have more options to read a file depending on what your needs are or how bigger it’s the file from where you read.
You can use the following code to read the file character by character(this version that is not faster to read files):

while not eof(f) do
begin
read(f,ch);
s:=s+ch;
end;

You can also use

readln(f,s)

which will read a whole line from the input file.

In the end you should close the file that you opened.

closefile(f);

Complete code, function to return contents of a file:

function ReadSmallFile:string;
var freadfile:textfile;
s,path:string;
ch:char;
 
begin
path:= ExtractFilePath(application.exename);
assignfile(freadfile,path+'filename.dat');
reset(freadfile);
while not eof(freadfile) do
begin
read(freadfile,ch);
s:=s+ch;
end;
closefile(freadfile);
ReadSmallFile:=s;
end;

Read bigger file in Delphi

For reading bigger files in Delphi you should use the function BlockRead. This function is used to read blocks of data into a buffer from a file.
To declare a file you should use:

var biggerfile:file of char;
BufArray: array[1..4096] of Char;//we will read 4 KB at a time
nrcit,i,:integer;
sir:string;

You should after assign to a variable just like the type TextFile:

assignfile(biggerfile,path+'namefile.dat');
reset(biggerfile);

Here is a statement to read a bigger file of type char

repeat
blockread(biggerfile,BufArray,SizeOf(BufArray),nrcit);
for i:=1 to nrcit do
sir:=sir+BufArray[i];
until (nrcit = 0);

To close the file you should use

closefile(fis);

Here is a complete source code function to read a bigger file:

function ReadBiggerFile:string;
var biggerfile:file of char;
BufArray: array[1..4096] of Char;//we will read 4 KB at a time
nrcit,i:integer;
sir,path:string;
begin
path:= ExtractFilePath(application.exename);
assignfile(biggerfile,path+'namefile.dat');
reset(biggerfile);
repeat
blockread(biggerfile,BufArray,SizeOf(BufArray),nrcit);
for i:=1 to nrcit do
sir:=sir+BufArray[i];
until (nrcit = 0);
closefile(biggerfile);
ReadBiggerFile:=sir;
end;

Tags: , ,
Posted in Delphi | No Comments »