An example of Image Storage and browsing (Linux + Apache + PHP + MySQL)

Source: Internet
Author: User

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;
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.