当前位置: 编程技术>php
本页文章导读:
▪PHP中的正规表达式(二)
确定重复出现到现在为止,你已经知道如何去匹配一个字母或数字,但更多的情况下,可能要匹配一个单词或一组数字。一个单词有若干个字母组成,一组数字有若干个单数组成。跟在字符.........
▪smtp邮件发送一例
test_smtp.php<?require("smtp.php");$smtp=new smtp_class;$smtp->host_name="mail.xiaocui.com";$smtp->localhost="localhost";$from="webmaster@xiaocui.com";$to="root@xiaocui.com";if($smtp->SendMessage( $from, array( $to ), ar.........
▪图片存储与浏览一例(Linux+Apache+PHP+MySQL)
注意本程序使用的表结构为: use test; create table image( id int unsigned auto_increment primary key, description text, .........
[1]PHP中的正规表达式(二)
来源: 互联网 发布时间: 2013-11-30
确定重复出现
到现在为止,你已经知道如何去匹配一个字母或数字,但更多的情况下,可能要匹配一个单词或一组数字。一个单词有若干个字母组成,一组数字有若干个单数组成。跟在字符或字符簇后面的花括号({})用来确定前面的内容的重复出现的次数。
字符簇 含义
^[a-zA-Z_]$ 所有的字母和下划线
^[[:alpha:]]{3}$ 所有的3个字母的单词
^a$ 字母a
^a{4}$ aaaa
^a{2,4}$ aa,aaa或aaaa
^a{1,3}$ a,aa或aaa
^a{2,}$ 包含多于两个a的字符串
^a{2,} 如:aardvark和aaab,但apple不行
a{2,} 如:baad和aaa,但Nantucket不行
\t{2} 两个制表符
.{2} 所有的两个字符
这些例子描述了花括号的三种不同的用法。一个数字,{x}的意思是“前面的字符或字符簇只出现x次”;一个数字加逗号,{x,}的意思是“前面的内容出现x或更多的次数”;两个用逗号分隔的数字,{x,y}表示“前面的内容至少出现x次,但不超过y次”。我们可以把模式扩展到更多的单词或数字:
^[a-zA-Z0-9_]{1,}$ //所有包含一个以上的字母、数字或下划线的字符串
^[0-9]{1,}$ //所有的正数
^\-{0,1}[0-9]{1,}$ //所有的整数
^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$ //所有的小数
最后一个例子不太好理解,是吗?这么看吧:与所有以一个可选的负号(\-{0,1})开头(^)、跟着0个或更多的数字([0-9]{0,})、和一个可选的小数点(\.{0,1})再跟上0个或多个数字([0-9]{0,}),并且没有其他任何东西($)。下面你将知道能够使用的更为简单的方法。
特殊字符"?"与{0,1}是相等的,它们都代表着:“0个或1个前面的内容”或“前面的内容是可选的”。所以刚才的例子可以简化为:
^\-?[0-9]{0,}\.?[0-9]{0,}$
特殊字符"*"与{0,}是相等的,它们都代表着“0个或多个前面的内容”。最后,字符"+"与 {1,}是相等的,表示“1个或多个前面的内容”,所以上面的4个例子可以写成:
^[a-zA-Z0-9_]+$ //所有包含一个以上的字母、数字或下划线的字符串
^[0-9]+$ //所有的正数
^\-?[0-9]+$ //所有的整数
^\-?[0-9]*\.?[0-9]*$ //所有的小数
当然这并不能从技术上降低正规表达式的复杂性,但可以使它们更容易阅读。
到现在为止,你已经知道如何去匹配一个字母或数字,但更多的情况下,可能要匹配一个单词或一组数字。一个单词有若干个字母组成,一组数字有若干个单数组成。跟在字符或字符簇后面的花括号({})用来确定前面的内容的重复出现的次数。
字符簇 含义
^[a-zA-Z_]$ 所有的字母和下划线
^[[:alpha:]]{3}$ 所有的3个字母的单词
^a$ 字母a
^a{4}$ aaaa
^a{2,4}$ aa,aaa或aaaa
^a{1,3}$ a,aa或aaa
^a{2,}$ 包含多于两个a的字符串
^a{2,} 如:aardvark和aaab,但apple不行
a{2,} 如:baad和aaa,但Nantucket不行
\t{2} 两个制表符
.{2} 所有的两个字符
这些例子描述了花括号的三种不同的用法。一个数字,{x}的意思是“前面的字符或字符簇只出现x次”;一个数字加逗号,{x,}的意思是“前面的内容出现x或更多的次数”;两个用逗号分隔的数字,{x,y}表示“前面的内容至少出现x次,但不超过y次”。我们可以把模式扩展到更多的单词或数字:
^[a-zA-Z0-9_]{1,}$ //所有包含一个以上的字母、数字或下划线的字符串
^[0-9]{1,}$ //所有的正数
^\-{0,1}[0-9]{1,}$ //所有的整数
^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$ //所有的小数
最后一个例子不太好理解,是吗?这么看吧:与所有以一个可选的负号(\-{0,1})开头(^)、跟着0个或更多的数字([0-9]{0,})、和一个可选的小数点(\.{0,1})再跟上0个或多个数字([0-9]{0,}),并且没有其他任何东西($)。下面你将知道能够使用的更为简单的方法。
特殊字符"?"与{0,1}是相等的,它们都代表着:“0个或1个前面的内容”或“前面的内容是可选的”。所以刚才的例子可以简化为:
^\-?[0-9]{0,}\.?[0-9]{0,}$
特殊字符"*"与{0,}是相等的,它们都代表着“0个或多个前面的内容”。最后,字符"+"与 {1,}是相等的,表示“1个或多个前面的内容”,所以上面的4个例子可以写成:
^[a-zA-Z0-9_]+$ //所有包含一个以上的字母、数字或下划线的字符串
^[0-9]+$ //所有的正数
^\-?[0-9]+$ //所有的整数
^\-?[0-9]*\.?[0-9]*$ //所有的小数
当然这并不能从技术上降低正规表达式的复杂性,但可以使它们更容易阅读。
[2]smtp邮件发送一例
来源: 互联网 发布时间: 2013-11-30
test_smtp.php
<?
require("smtp.php");
$smtp=new smtp_class;
$smtp->host_name="mail.xiaocui.com";
$smtp->localhost="localhost";
$from="webmaster@xiaocui.com";
$to="root@xiaocui.com";
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class"
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"))
echo "Message sent to $to OK.\n";
else
echo "Cound not send the message to $to.\nError: ".$smtp->error."\n"
?>
smtp.php
<?
class smtp_class
{
var $host_name="";
var $host_port=25;
var $localhost="";
var $timeout=0;
var $error="";
var $debug=1;
var $esmtp=1;
var $esmtp_host="";
var $esmtp_extensions=array();
var $maximum_piped_recipients=100;
/* private variables - DO NOT ACCESS */
var $state="Disconnected";
var $connection=0;
var $pending_recipients=0;
/* Private methods - DO NOT CALL */
Function OutputDebug($message)
{
echo $message,"<br>\n";
}
Function GetLine()
{
for($line="";;)
{
if(feof($this->connection))
{
$this->error="reached the end of stream while reading from socket";
return(0);
}
if(($data=fgets($this->connection,100))==false)
{
$this->error="it was not possible to read line from socket";
return(0);
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="\r\n")
{
$line=substr($line,0,$length-2);
if($this->debug)
$this->OutputDebug("< $line");
return($line);
}
}
}
Function PutLine($line)
{
if($this->debug)
$this->OutputDebug("> $line");
if(!fputs($this->connection,"$line\r\n"))
{
$this->error="it was not possible to write line to socket";
return(0);
}
return(1);
}
Function PutData($data)
{
if(strlen($data))
{
if($this->debug)
$this->OutputDebug("> $data");
if(!fputs($this->connection,$data))
{
$this->error="it was not possible to write data to socket";
return(0);
}
}
return(1);
}
Function VerifyResultLines($code,$responses="")
{
if(GetType($responses)!="array")
$responses=array();
Unset($match_code);
while(($line=$this->GetLine($this->connection)))
{
if(IsSet($match_code))
{
if(strcmp(strtok($line," -"),$match_code))
{
$this->error=$line;
return(0);
}
}
else
{
$match_code=strtok($line," -");
if(GetType($code)=="array")
{
for($codes=0;$codes<count($code) && strcmp($match_code,$code[$codes]);$codes++);
if($codes>=count($code))
{
$this->error=$line;
return(0);
}
}
else
{
if(strcmp($match_code,$code))
{
$this->error=$line;
return(0);
}
}
}
$responses[]=strtok("");
if(!strcmp($match_code,strtok($line," ")))
return(1);
}
return(-1);
}
Function FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250")<=0)
return(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
return(1);
}
/* Public methods */
Function Connect()
{
$this->error=$error="";
$this->esmtp_host="";
$this->esmtp_extensions=array();
if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
{
switch($error)
{
case -3:
$this->error="-3 socket could not be created";
return(0);
case -4:
$this->error="-4 dns lookup on hostname \"".$host_name."\" failed";
return(0);
case -5:
$this->error="-5 connection refused or timed out";
return(0);
case -6:
$this->error="-6 fdopen() call failed";
return(0);
case -7:
$this->error="-7 setvbuf() call failed";
return(0);
default:
$this->error=$error." could not connect to the host \"".$this->host_name."\"";
return(0);
}
}
else
{
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$success=0;
if($this->VerifyResultLines("220")>0)
{
if($this->esmtp)
{
$responses=array();
if($this->PutLine("EHLO $localhost")
&& $this->VerifyResultLines("250",&$responses)>0)
{
$this->esmtp_host=strtok($responses[0]," ");
for($response=1;$response<count($responses);$response++)
{
$extension=strtoupper(strtok($responses[$response]," "));
$this->esmtp_extensions[$extension]=strtok("");
}
$success=1;
}
}
if(!$success
&& $this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250")>0)
$success=1;
}
if($success)
{
$this->state="Connected";
return(1);
}
else
{
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(0);
}
}
}
Function MailFrom($sender)
{
if(strcmp($this->state,"Connected"))
{
$this->error="connection is not in the initial state";
return(0);
}
$this->error="";
if(!$this->PutLine("MAIL FROM: <".$sender.">"))
return(0);
if(!IsSet($this->esmtp_extensions["PIPELINING"])
&& $this->VerifyResultLines("250")<=0)
return(0);
$this->state="SenderSet";
if(IsSet($this->esmtp_extensions["PIPELINING"]))
$this->pending_sender=1;
$this->pending_recipients=0;
return(1);
}
Function SetRecipient($recipient)
{
switch($this->state)
{
case "SenderSet":
case "RecipientSet":
break;
default:
$this->error="connection is not in the recipient setting state";
return(0);
}
$this->error="";
if(!$this->PutLine("RCPT TO:<".$recipient.">"))
return(0);
if(IsSet($this->esmtp_extensions["PIPELINING"]))
{
$this->pending_recipients++;
if($this->pending_recipients>=$this->maximum_piped_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
}
else
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
$this->state="RecipientSet";
return(1);
}
Function StartData()
{
if(strcmp($this->state,"RecipientSet"))
{
$this->error="connection is not in the start sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("DATA"))
return(0);
if($this->pending_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
if($this->VerifyResultLines("354")<=0)
return(0);
$this->state="SendingData";
return(1);
}
Function PrepareData($data,&$output)
{
$length=strlen(&$data);
for($output="",$position=0;$position<$length;)
{
$next_position=$length;
for($current=$position;$current<$length;$current++)
{
switch($data[$current])
{
case "\n":
$next_position=$current+1;
break 2;
case "\r":
$next_position=$current+1;
if($data[$next_position]=="\n")
$next_position++;
break 2;
}
}
if($data[$position]==".")
$output.=".";
$output.=substr(&$data,$position,$current-$position)."\r\n";
$position=$next_position;
}
}
Function SendData($data)
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
return($this->PutData(&$data));
}
Function EndSendingData()
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("\r\n.")
|| $this->VerifyResultLines("250")<=0)
return(0);
$this->state="Connected";
return(1);
}
Function ResetConnection()
{
switch($this->state)
{
case "Connected":
return(1);
case "SendingData":
$this->error="can not reset the connection while sending data";
return(0);
case "Disconnected":
$this->error="can not reset the connection before it is established";
return(0);
}
$this->error="";
if(!$this->PutLine("RSET")
|| $this->VerifyResultLines("250")<=0)
return(0);
$this->state="Connected";
return(1);
}
Function Disconnect($quit=1)
{
if(!strcmp($this->state,"Disconnected"))
{
$this->error="it was not previously established a SMTP connection";
return(0);
}
$this->error="";
if(!strcmp($this->state,"Connected")
&& $quit
&& (!$this->PutLine("QUIT")
|| $this->VerifyResultLines("221")<=0))
return(0);
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(1);
}
Function SendMessage($sender,$recipients,$headers,$body)
{
if(($success=$this->Connect()))
{
if(($success=$this->MailFrom($sender)))
{
for($recipient=0;$recipient<count($recipients);$recipient++)
{
if(!($success=$this->SetRecipient($recipients[$recipient])))
break;
}
if($success
&& ($success=$this->StartData()))
{
for($header_data="",$header=0;$header<count($headers);$header++)
$header_data.=$headers[$header]."\r\n";
if(($success=$this->SendData($header_data."\r\n")))
{
$this->PrepareData($body,&$body_data);
$success=$this->SendData($body_data);
}
if($success)
$success=$this->EndSendingData();
}
}
$disconnect_success=$this->Disconnect($success);
if($success)
$success=$disconnect_success;
}
return($success);
}
};
?>
<?
require("smtp.php");
$smtp=new smtp_class;
$smtp->host_name="mail.xiaocui.com";
$smtp->localhost="localhost";
$from="webmaster@xiaocui.com";
$to="root@xiaocui.com";
if($smtp->SendMessage(
$from,
array(
$to
),
array(
"From: $from",
"To: $to",
"Subject: Testing Manuel Lemos' SMTP class"
),
"Hello $to,\n\nIt is just to let you know that your SMTP class is working just fine.\n\nBye.\n"))
echo "Message sent to $to OK.\n";
else
echo "Cound not send the message to $to.\nError: ".$smtp->error."\n"
?>
smtp.php
<?
class smtp_class
{
var $host_name="";
var $host_port=25;
var $localhost="";
var $timeout=0;
var $error="";
var $debug=1;
var $esmtp=1;
var $esmtp_host="";
var $esmtp_extensions=array();
var $maximum_piped_recipients=100;
/* private variables - DO NOT ACCESS */
var $state="Disconnected";
var $connection=0;
var $pending_recipients=0;
/* Private methods - DO NOT CALL */
Function OutputDebug($message)
{
echo $message,"<br>\n";
}
Function GetLine()
{
for($line="";;)
{
if(feof($this->connection))
{
$this->error="reached the end of stream while reading from socket";
return(0);
}
if(($data=fgets($this->connection,100))==false)
{
$this->error="it was not possible to read line from socket";
return(0);
}
$line.=$data;
$length=strlen($line);
if($length>=2
&& substr($line,$length-2,2)=="\r\n")
{
$line=substr($line,0,$length-2);
if($this->debug)
$this->OutputDebug("< $line");
return($line);
}
}
}
Function PutLine($line)
{
if($this->debug)
$this->OutputDebug("> $line");
if(!fputs($this->connection,"$line\r\n"))
{
$this->error="it was not possible to write line to socket";
return(0);
}
return(1);
}
Function PutData($data)
{
if(strlen($data))
{
if($this->debug)
$this->OutputDebug("> $data");
if(!fputs($this->connection,$data))
{
$this->error="it was not possible to write data to socket";
return(0);
}
}
return(1);
}
Function VerifyResultLines($code,$responses="")
{
if(GetType($responses)!="array")
$responses=array();
Unset($match_code);
while(($line=$this->GetLine($this->connection)))
{
if(IsSet($match_code))
{
if(strcmp(strtok($line," -"),$match_code))
{
$this->error=$line;
return(0);
}
}
else
{
$match_code=strtok($line," -");
if(GetType($code)=="array")
{
for($codes=0;$codes<count($code) && strcmp($match_code,$code[$codes]);$codes++);
if($codes>=count($code))
{
$this->error=$line;
return(0);
}
}
else
{
if(strcmp($match_code,$code))
{
$this->error=$line;
return(0);
}
}
}
$responses[]=strtok("");
if(!strcmp($match_code,strtok($line," ")))
return(1);
}
return(-1);
}
Function FlushRecipients()
{
if($this->pending_sender)
{
if($this->VerifyResultLines("250")<=0)
return(0);
$this->pending_sender=0;
}
for(;$this->pending_recipients;$this->pending_recipients--)
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
return(1);
}
/* Public methods */
Function Connect()
{
$this->error=$error="";
$this->esmtp_host="";
$this->esmtp_extensions=array();
if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
{
switch($error)
{
case -3:
$this->error="-3 socket could not be created";
return(0);
case -4:
$this->error="-4 dns lookup on hostname \"".$host_name."\" failed";
return(0);
case -5:
$this->error="-5 connection refused or timed out";
return(0);
case -6:
$this->error="-6 fdopen() call failed";
return(0);
case -7:
$this->error="-7 setvbuf() call failed";
return(0);
default:
$this->error=$error." could not connect to the host \"".$this->host_name."\"";
return(0);
}
}
else
{
if(!strcmp($localhost=$this->localhost,"")
&& !strcmp($localhost=getenv("SERVER_NAME"),"")
&& !strcmp($localhost=getenv("HOST"),""))
$localhost="localhost";
$success=0;
if($this->VerifyResultLines("220")>0)
{
if($this->esmtp)
{
$responses=array();
if($this->PutLine("EHLO $localhost")
&& $this->VerifyResultLines("250",&$responses)>0)
{
$this->esmtp_host=strtok($responses[0]," ");
for($response=1;$response<count($responses);$response++)
{
$extension=strtoupper(strtok($responses[$response]," "));
$this->esmtp_extensions[$extension]=strtok("");
}
$success=1;
}
}
if(!$success
&& $this->PutLine("HELO $localhost")
&& $this->VerifyResultLines("250")>0)
$success=1;
}
if($success)
{
$this->state="Connected";
return(1);
}
else
{
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(0);
}
}
}
Function MailFrom($sender)
{
if(strcmp($this->state,"Connected"))
{
$this->error="connection is not in the initial state";
return(0);
}
$this->error="";
if(!$this->PutLine("MAIL FROM: <".$sender.">"))
return(0);
if(!IsSet($this->esmtp_extensions["PIPELINING"])
&& $this->VerifyResultLines("250")<=0)
return(0);
$this->state="SenderSet";
if(IsSet($this->esmtp_extensions["PIPELINING"]))
$this->pending_sender=1;
$this->pending_recipients=0;
return(1);
}
Function SetRecipient($recipient)
{
switch($this->state)
{
case "SenderSet":
case "RecipientSet":
break;
default:
$this->error="connection is not in the recipient setting state";
return(0);
}
$this->error="";
if(!$this->PutLine("RCPT TO:<".$recipient.">"))
return(0);
if(IsSet($this->esmtp_extensions["PIPELINING"]))
{
$this->pending_recipients++;
if($this->pending_recipients>=$this->maximum_piped_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
}
else
{
if($this->VerifyResultLines(array("250","251"))<=0)
return(0);
}
$this->state="RecipientSet";
return(1);
}
Function StartData()
{
if(strcmp($this->state,"RecipientSet"))
{
$this->error="connection is not in the start sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("DATA"))
return(0);
if($this->pending_recipients)
{
if(!$this->FlushRecipients())
return(0);
}
if($this->VerifyResultLines("354")<=0)
return(0);
$this->state="SendingData";
return(1);
}
Function PrepareData($data,&$output)
{
$length=strlen(&$data);
for($output="",$position=0;$position<$length;)
{
$next_position=$length;
for($current=$position;$current<$length;$current++)
{
switch($data[$current])
{
case "\n":
$next_position=$current+1;
break 2;
case "\r":
$next_position=$current+1;
if($data[$next_position]=="\n")
$next_position++;
break 2;
}
}
if($data[$position]==".")
$output.=".";
$output.=substr(&$data,$position,$current-$position)."\r\n";
$position=$next_position;
}
}
Function SendData($data)
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
return($this->PutData(&$data));
}
Function EndSendingData()
{
if(strcmp($this->state,"SendingData"))
{
$this->error="connection is not in the sending data state";
return(0);
}
$this->error="";
if(!$this->PutLine("\r\n.")
|| $this->VerifyResultLines("250")<=0)
return(0);
$this->state="Connected";
return(1);
}
Function ResetConnection()
{
switch($this->state)
{
case "Connected":
return(1);
case "SendingData":
$this->error="can not reset the connection while sending data";
return(0);
case "Disconnected":
$this->error="can not reset the connection before it is established";
return(0);
}
$this->error="";
if(!$this->PutLine("RSET")
|| $this->VerifyResultLines("250")<=0)
return(0);
$this->state="Connected";
return(1);
}
Function Disconnect($quit=1)
{
if(!strcmp($this->state,"Disconnected"))
{
$this->error="it was not previously established a SMTP connection";
return(0);
}
$this->error="";
if(!strcmp($this->state,"Connected")
&& $quit
&& (!$this->PutLine("QUIT")
|| $this->VerifyResultLines("221")<=0))
return(0);
fclose($this->connection);
$this->connection=0;
$this->state="Disconnected";
return(1);
}
Function SendMessage($sender,$recipients,$headers,$body)
{
if(($success=$this->Connect()))
{
if(($success=$this->MailFrom($sender)))
{
for($recipient=0;$recipient<count($recipients);$recipient++)
{
if(!($success=$this->SetRecipient($recipients[$recipient])))
break;
}
if($success
&& ($success=$this->StartData()))
{
for($header_data="",$header=0;$header<count($headers);$header++)
$header_data.=$headers[$header]."\r\n";
if(($success=$this->SendData($header_data."\r\n")))
{
$this->PrepareData($body,&$body_data);
$success=$this->SendData($body_data);
}
if($success)
$success=$this->EndSendingData();
}
}
$disconnect_success=$this->Disconnect($success);
if($success)
$success=$disconnect_success;
}
return($success);
}
};
?>
[3]图片存储与浏览一例(Linux+Apache+PHP+MySQL)
来源: 互联网 发布时间: 2013-11-30
注意本程序使用的表结构为:
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}
//检查cmd参数的合法性
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={}
//读一个图片
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$sql = "select filetype,filedata from image where id='$id'";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
if($row=mysql_fetch_row($rst)){
header("Content-Type:" . $row[0]);
echo $row[1];
}
else{
echo "没有找到该记录";
}
mysql_free_result($rst);
mysql_close($server) or die("无法与数据库服务器断开连接");
break;
case 'list':
//?cmd=list
//显示所有图片
echo '<html>';
echo '<head><title>图片存储与浏览一例</title></head>';
echo '<body>';
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
echo " ";
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$sql = "select id,description,filename,filetype,filesize from image";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
while($row=mysql_fetch_row($rst)){
echo "<hr>";
echo "描述:" . $row[1] . "<br>";
echo "文件名:" . $row[2] . "<br>";
echo "类型:" . $row[3] . "<br>";
echo "大小:" . $row[4] . "<br>";
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $row[0] . '">';
}
mysql_free_result($rst);
mysql_close($server) or die("无法与数据库服务器断开连接");
echo '</body>';
echo '</html>';
break;
case 'form':
?>
<html>
<head><title>图片存储与浏览一例</title></head>
<body>
<form action="/blog_article/</echo $PHP_SELF;/gt;cmd/store.html" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
描述:<br>
<textarea name="description" rows="5" cols="100"></textarea><br>
文件:<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
<?
break;
case 'store':
//?cmd=store&description={}&file={}&file_size={}&file_type={}&file_name={}
//存储图片
echo '<html>';
echo '<head><title>图片存储与浏览一例</title></head>';
echo '<body>';
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
echo " ";
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$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执行出错");
$id = mysql_insert_id();
echo "<hr>你上传的图片效果:<br>";
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $id . '">';
mysql_close($server) or die("无法与数据库服务器断开连接");
echo '</body>';
echo '</html>';
break;
}
?>
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}
//检查cmd参数的合法性
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={}
//读一个图片
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$sql = "select filetype,filedata from image where id='$id'";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
if($row=mysql_fetch_row($rst)){
header("Content-Type:" . $row[0]);
echo $row[1];
}
else{
echo "没有找到该记录";
}
mysql_free_result($rst);
mysql_close($server) or die("无法与数据库服务器断开连接");
break;
case 'list':
//?cmd=list
//显示所有图片
echo '<html>';
echo '<head><title>图片存储与浏览一例</title></head>';
echo '<body>';
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
echo " ";
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$sql = "select id,description,filename,filetype,filesize from image";
$rst = mysql_query($sql,$server) or die("$sql查询出错");
while($row=mysql_fetch_row($rst)){
echo "<hr>";
echo "描述:" . $row[1] . "<br>";
echo "文件名:" . $row[2] . "<br>";
echo "类型:" . $row[3] . "<br>";
echo "大小:" . $row[4] . "<br>";
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $row[0] . '">';
}
mysql_free_result($rst);
mysql_close($server) or die("无法与数据库服务器断开连接");
echo '</body>';
echo '</html>';
break;
case 'form':
?>
<html>
<head><title>图片存储与浏览一例</title></head>
<body>
<form action="/blog_article/</echo $PHP_SELF;/gt;cmd/store.html" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
描述:<br>
<textarea name="description" rows="5" cols="100"></textarea><br>
文件:<input type="file" name="file"><br>
<input type="submit" value="上传">
</form>
</body>
</html>
<?
break;
case 'store':
//?cmd=store&description={}&file={}&file_size={}&file_type={}&file_name={}
//存储图片
echo '<html>';
echo '<head><title>图片存储与浏览一例</title></head>';
echo '<body>';
echo '<a href="' . $PHP_SELF . '?cmd=list">显示所有图片</a>';
echo " ";
echo '<a href="' . $PHP_SELF . '?cmd=form">上传图片</a>';
$server = mysql_connect("localhost","test","") or die("无法连接数据库服务器");
mysql_select_db("test",$server) or die("无法连接数据库");
$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执行出错");
$id = mysql_insert_id();
echo "<hr>你上传的图片效果:<br>";
echo '<img src="' . $PHP_SELF . '?cmd=read&id=' . $id . '">';
mysql_close($server) or die("无法与数据库服务器断开连接");
echo '</body>';
echo '</html>';
break;
}
?>
最新技术文章: