Note that the table structure used in this program is:
Use test;
Create table image (
Id int unsigned auto_increment primary key,
Description text,
Filename varchar (50 ),
Filesize int,
Filetype varchar (50 ),
Filedata longblob
);
*/
//? Cmd = {read | list | form | store}
// Check the validity of the cmd Parameter
Switch ($ cmd ){
Case 'read ':
Break;
Case 'LIST ':
Break;
Case 'form ':
Break;
Case 'store ':
Break;
Default:
$ Cmd = 'LIST ';
Break;
}
Switch ($ cmd ){
Case 'read ':
//? Cmd = read & id = {}
// Read an image
$ Server = mysql_connect ("localhost", "test", "") or die ("unable to connect to the database server ");
Mysql_select_db ("test", $ server) or die ("unable to connect to Database ");
$ SQL = "select filetype, filedata from image where id = '$ id '";
$ Rst = mysql_query ($ SQL, $ server) or die ("$ SQL query error ");
If ($ row = mysql_fetch_row ($ rst )){
Header ("Content-Type:". $ row [0]);
Echo $ row [1];
}
Else {
Echo "this record is not found ";
}
Mysql_free_result ($ rst );
Mysql_close ($ server) or die ("unable to disconnect from database server ");
Break;
Case 'LIST ':
//? Cmd = list
// Display all images
Echo 'Echo 'Echo '<body> ';
Echo '<a href = "'. $ PHP_SELF .'? Cmd = list "> show all images </a> ';
Echo "";
Echo '<a href = "'. $ PHP_SELF .'? Cmd = form "> upload an image </a> ';
$ Server = mysql_connect ("localhost", "test", "") or die ("unable to connect to the database server ");
Mysql_select_db ("test", $ server) or die ("unable to connect to Database ");
$ SQL = "select id, description, filename, filetype, filesize from image ";
$ Rst = mysql_query ($ SQL, $ server) or die ("$ SQL query error ");
While ($ row = mysql_fetch_row ($ rst )){
Echo "Echo "Description:". $ row [1]. "<br> ";
Echo "file name:". $ row [2]. "<br> ";
Echo "type:". $ row [3]. "<br> ";
Echo "Size:". $ row [4]. "<br> ";
Echo ' ';
}
Mysql_free_result ($ rst );
Mysql_close ($ server) or die ("unable to disconnect from database server ");
Echo '</body> ';
Echo 'Break;
Case 'form ':
?>
<Html>
<Head> <title> image storage and browsing example </title> <Body>
<Form action = "<? Echo $ PHP_SELF;?>? Cmd = store "method =" post "enctype =" multipart/form-data ">
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "2097152">
Description: <br>
<Textarea name = "description" rows = "5" cols = "100"> </textarea> <br>
File: <input type = "file" name = "file"> <br>
<Input type = "submit" value = "Upload">
</Form>
</Body>
</Html>
<?
Break;
Case 'store ':
//? Cmd = store & description ={}& file ={} & file_size ={} & file_type ={} & file_name = {}
// Store images
Echo 'Echo 'Echo '<body> ';
Echo '<a href = "'. $ PHP_SELF .'? Cmd = list "> show all images </a> ';
Echo "";
Echo '<a href = "'. $ PHP_SELF .'? Cmd = form "> upload an image </a> ';
$ Server = mysql_connect ("localhost", "test", "") or die ("unable to connect to the database server ");
Mysql_select_db ("test", $ server) or die ("unable to connect to Database ");
$ Data = addslashes (fread (fopen ($ file, "r"), filesize ($ file )));
$ SQL = "insert into image (description, filename, filetype, filesize, filedata)
Values ('$ description',' ". basename ($ file_name)." ',' $ file_type ', $ file_size,' $ data ')";
Mysql_query ($ SQL, $ server) or die ("$ SQL Execution error ");
$ Id = mysql_insert_id ();
Echo "Echo ' ';
Mysql_close ($ server) or die ("unable to disconnect from database server ");
Echo '</body> ';
Echo 'Break;
}
?>