Image storage and browsing case (LINUX+APACHE+PHP+MYSQL) _php Foundation

Source: Internet
Author: User
Note the table structure used by 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 legality 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 a picture
$server = mysql_connect ("localhost", "Test", "") or Die ("Unable to connect to database Server");
mysql_select_db ("Test", $server) or Die ("Cannot connect to the 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 "did not find the record";
}
Mysql_free_result ($rst);
Mysql_close ($server) or Die ("Cannot disconnect from the database server");
Break
Case ' list ':
? cmd=list
Show All pictures
Echo ' Echo ' Echo ' <body> ';
Echo ' <a href= '. $PHP _self. '? cmd=list ' > Show all Pictures </a> ';
echo "";
Echo ' <a href= '. $PHP _self. '? cmd=form ' > Upload picture </a> ';
$server = mysql_connect ("localhost", "Test", "") or Die ("Unable to connect to database Server");
mysql_select_db ("Test", $server) or Die ("Cannot connect to the 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 "FileName:". $row [2]. "<br>";
echo "Type:". $row [3]. "<br>";
echo "Size:". $row [4]. "<br>";
Echo ' ';
}
Mysql_free_result ($rst);
Mysql_close ($server) or Die ("Cannot disconnect from the database server");
Echo ' </body> ';
Echo ' Break
Case ' form ':
?>

<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= "></textarea><br>"
File: <input type= "file" name= "file" ><br>
<input type= "Submit" value= "Upload" >
</form>
</body>

?
Break
Case ' Store ':
? cmd=store&description={}&file={}&file_size={}&file_type={}&file_name={}
Storing pictures
Echo ' Echo ' Echo ' <body> ';
Echo ' <a href= '. $PHP _self. '? cmd=list ' > Show all Pictures </a> ';
echo "";
Echo ' <a href= '. $PHP _self. '? cmd=form ' > Upload picture </a> ';
$server = mysql_connect ("localhost", "Test", "") or Die ("Unable to connect to database Server");
mysql_select_db ("Test", $server) or Die ("Cannot connect to the 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 ("Error $sql execution");
$id = mysql_insert_id ();
echo "Echo ' ';
Mysql_close ($server) or Die ("Cannot disconnect from the 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.