Thursday, June 11, 2009

Uploading Files To MySQL Database

Uploading Files To MySQL Database:

Using PHP to upload files into MySQL database sometimes needed by some web application. For instance for storing pdf documents or images to make som kind of online briefcase (like Yahoo briefcase).

For the first step, let's make the table for the upload files. The table will consist of.

1. id : Unique id for each file
2. name : File name
3. type : File content type
4. size : File size
5. content : The file itself



For column content we'll use BLOB data type. BLOB is a binary large object that can hold a variable amount of data. MySQL have four BLOB data types, they are :

* TINYBLOB
* BLOB
* MEDIUMBLOB
* LONGBLOB

Since BLOB is limited to store up to 64 kilobytes of data we will use MEDIUMBLOB so we can store larger files ( up to 16 megabytes ).
CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);

Uploading a file to MySQL is a two step process. First you need to upload the file to the server then read the file and insert it to MySQL.

For uploading a file we need a form for the user to enter the file name or browse their computer and select a file. The input type="file" is used for that purpose.



Example : upload.php
Source code : upload.phps

An upload form must have encytype="multipart/form-data" otherwise it won't work at all. Of course the form method also need to be set to method="post". Also remember to put a hidden input MAX_FILE_SIZE before the file input. It's to restrict the size of files.

After the form is submitted the we need to read the autoglobal $_FILES. In the example above the input name for the file is userfile so the content of $_FILES are like this :

$_FILES['userfile']['name']
The original name of the file on the client machine.

$_FILES['userfile']['type']
The mime type of the file, if the browser provided this information. An example would be "image/gif".

$_FILES['userfile']['size']
The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['userfile']['error']
The error code associated with this file upload. ['error'] was added in PHP 4.2.0

Example : upload.php


if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';

echo "
File $fileName uploaded
";
}
?>
Before you do anything with the uploaded file. You should not assume that the file was uploaded successfully to the server. Always check to see if the file was successfully uploaded by looking at the file size. If it's larger than zero byte then we can assume that the file is uploaded successfully.

PHP saves the uploaded file with a temporary name and save the name in $_FILES['userfile']['tmp_name']. Our next job is to read the content of this file and insert the content to database. Always make sure that you use addslashes() to escape the content. Using addslashes() to the file name is also recommended because you never know what the file name would be.

That's it now you can upload your files to MySQL. Now it's time to write the script to download those files.

Saturday, June 6, 2009

Waikato Environment for Knowledge analysis (WEKA)

WEKA?:

Waka is a collection of machine learning algorithms for solving real-world data mining problems. It is written in Java and runs on almost every platform. You can access the WEKA class library from your own Java program, and implement new machine learning algorithms.

schemes in WEKA:

There are three major implemented schemes in WEKA--
(1) Implemented schemes for classification
(2) Implemented schemes for numeric prediction
(3) Implemented "meta-schemes"

WEKA also contains a large variety of tools that can be used for pre-processing datasets, so that you can focus on your algorithm without considering too much details as reading the data from files, implementing filtering algorithm and providing code to evaluate the results.

How to install and run WEKA on CS account?:

1) Log into your CS account.

2) Create a directory called weka using:

> mkdir weka

> cd weka

3) Create a symbolic link to our installed weka pack:

> ln -s /home/course/cs573x/weka-3-4/weka.jar

4) You are able to run weka now:

> java -jar weka.jar

*note: this will only work on Linux or Solaris workstations, as this will run GUI version of WEKA

5) Please make the following two links to enable you to access the source code of weka and some sample datasets.

> ln -s /home/course/cs573x/weka-3-4/weka

> ln -s /home/course/cs573x/weka-3-4/data

The following step is to set the CLASSPATH environment variable prior to use the command line. You need to indicate the location of the weka.jar file.

1) For sh, ksh and bash users:

Please add "export CLASSPATH=/home/course/cs573x/weka-3-4/weka.jar:$CLASSPATH" into your shell configuration profile.

2) For csh and tcsh users:

Please add "setenv CLASSPATH /home/course/cs573x/weka-3-4/weka.jar" into your shell configuration profile.

3) To test if it is set correctly:

Type "java weka.classifiers.trees.j48.J48 " (note the change from previous versions, in case you are familiar with any), it should display a list of all learning options for J48. If it displays an exception error message, then you will check if you set the environment variable correctly. Please make sure that you also set the correct path to Java, so that the system can locate and run Java.

Now, the installation is done! You can run WEKA either in command line or in graphic user interface. Remember only in Linux or a Solaris workstation can you run GUI, telnet won't work.