Home > Backend Development > PHP Tutorial > An example of image storage and browsing (Linux+Apache+PHP+MySQL)_PHP tutorial

An example of image storage and browsing (Linux+Apache+PHP+MySQL)_PHP tutorial

WBOY
Release: 2016-07-21 16:01:12
Original
924 people have browsed it

Note that the table structure used in this program is:
use test;
create table image(
filename varchar(50),
filesize int,
filetype varchar(50),
filedata longblob
);
*/

//?cmd={read|list|form|store}

//Check the validity of cmd parameters
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("Cannot connect to 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 "The record was not found";
}
mysql_free_result($rst);
mysql_close($server) or die("Unable to disconnect from database server");
break;
case 'list':
//? cmd=list
//Show all pictures
echo '';
echo 'An example of image storage and browsing ';
echo '';
echo 'Show all images';
echo " " ";
echo 'Upload pictures';
$server = mysql_connect("localhost"," test","") or die("Cannot connect to 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] . "
";
echo "File name:" . $row[2] . "
";
  echo "Type:" . $row[3] . "
";
  echo "Size:" . $row[4] . "
";
echo '';
}
mysql_free_result($rst);
mysql_close($server) or die("Unable to disconnect from database server");
echo '';
echo '';
break;
case 'form':
?>


An example of image storage and browsing



Description:



File:






break;
case 'store':
//?cmd=store&description={}&file={}&file_size={}&file_type={}&file_name={}
//Storing pictures
echo '';
echo 'An example of image storage and browsing';
echo '';
echo 'show all images';
echo " ";
echo 'Upload pictures';
$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 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("$sql execution error");
$id = mysql_insert_id ();
echo "
The effect of the picture you uploaded:
";
echo '';
mysql_close($server) or die("Unable to disconnect from the database server");
echo '';
echo ' break;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316853.htmlTechArticleNote 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 varcha...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template