谢谢matrix2000上面提供的函数列表!我有一篇文章,可能对你有用。-------------------------------------------
多文件上传
2000年08月24
<?
// 本例来自与ZEND网站,由HUNTE整理 // ASP-style tags are okay in PHP 3.0.4 and above; some HTML editors recognize these tags // File Upload Script for PHP/3 for Windows // Released under the terms of the public GNU license
// Based upon code written by Rasmus Lerdorf
// Modified for Windows by Michael Grier
// E-mail: [email protected] // You need to write-enable a directory, named "upload", below the one you place this script in
// On Windows NT, you can do this with a DOS program named CACLS
// Directions for this can be found at "http://www.bigmweb.com/home/cacls.html" if($action1){
// if files have been uploaded, process them ?>
<html>
<head>
<title>File Upload Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> <body bgcolor="#FFFFFF" text="#000000">
<p><font face="Arial, Helvetica, sans-serif"><font size="+1">File
Upload Results</font><br><br> <?
set_time_limit(60);
// make reasonably sure the script does not time out on large files
$path1 = dirname($PATH_TRANSLATED)."/upload/";
//print $PATH_TRANSLATED;
//print $path1."<br>";
// assign our path in a form PHP for Windows understands for($i=1;$i<3;$i++)
{
$temp1="file".$i;
$temp2="file".$i."_name";
$source=$$temp1;
$source_name=$$temp2;
// print $temp."=".$$temp."<br>"; //print $file1_name;
//print $file2_name;
//exit; //*****************
//$source = $file1;
//$source_name = $file1_name;
print $source."<br>";
print $source_name;
//exit;
if(($source <> "none")&&($source <> ""))
{
// see if the file exists; non-existing file has name of "none"
if($error1 <> 1)
{
// no need to copy a file if directory not write-enabled
$dest = $path1.$source_name;
// append file name to our path if(copy($source,$dest)){
// copy the file from the temp directory to the upload directory, and test for success
echo "$source has been uploaded<br>\n";
$imagesize = getimagesize($dest);
switch($imagesize[2])
{
case 0:
echo "<br> Image is a unknown <br>";
unlink($dest);
exit;
case 1:
echo "<br> Image is a GIF <br>";
echo "$dest has a width of $imagesize[0]<br>";
echo "$dest has a height of $imagesize[1]<br>";
$newname = $path1;
$newname .=$i.time() . ".gif";
if(copy($dest,$newname))
{
echo "<br> GIF Rename Successful from $dest to $newname";
}else {
echo "<br> GIF Rename Unsuccessful";
}
unlink ($dest);
break;
case 2:
echo "<br> Image is a JPG <br>";
echo "$dest has a width of $imagesize[0]<br>";
echo "$dest has a height of $imagesize[1]<br>";
$newname = $path1;
$newname .=$i.time() . ".jpg";
if(copy($dest,$newname))
{
echo "<br> JPG Rename Successful from $dest to $newname";
}else {
echo "<br> JPG Rename Unsuccessful";
}
unlink ($dest);
break;
case 3:
echo "<br> Image is a PNG <br>";
echo "$dest has a width of $imagesize[0]<br>";
echo "$dest has a height of $imagesize[1]<br>";
$newname = $path1;
$newname .=time() . ".png";
if(copy($dest,$newname))
{
echo "<br> PNG Rename Successful from $dest to $newname";
}else {
echo "<br> PNG Rename Unsuccessful";
}
unlink ($dest);
break;
}
}else {
echo "Upload directory not write-enabled\n";
// you need to write-enable the upload directory
$error1 = 1; // set flag
}
}
unlink($source);
// delete the file from the temp directory
} ?>
<image src="upload/<? print basename($newname)?>">
<?
} //end for
?>
<br><a href="fup.php">Back</a>
</font></p>
</body>
</html> <?
}
else { ?> <html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> <body bgcolor="#FFFFFF" text="#000000">
<p><font face="Arial, Helvetica, sans-serif"><font size="+1">File
Upload</font><br><br>
If your browser is upload-enabled, you will see "Browse"
(Netscape, Internet Explorer) or ". . ." (Opera) buttons below.
Use them to select file(s) to upload, then click the "Upload"
button. After the files have been uploaded, you will see a results screen.<br>
<form method="post" enctype="multipart/form-data" action="fup.php">
<input type="hidden" name="MAX_FILE_SIZE" value="800000">
<input type="hidden" name="action1" value="1">
File 1: <input type="file" name="file1" size="30"><br>
File 2: <input type="file" name="file2" size="30"><br>
<br>
<input type="submit" value="Upload">
</form>
</font></p>
</body>
</html> <? } ?>
多文件上传
2000年08月24
<?
// 本例来自与ZEND网站,由HUNTE整理 // ASP-style tags are okay in PHP 3.0.4 and above; some HTML editors recognize these tags // File Upload Script for PHP/3 for Windows // Released under the terms of the public GNU license
// Based upon code written by Rasmus Lerdorf
// Modified for Windows by Michael Grier
// E-mail: [email protected] // You need to write-enable a directory, named "upload", below the one you place this script in
// On Windows NT, you can do this with a DOS program named CACLS
// Directions for this can be found at "http://www.bigmweb.com/home/cacls.html" if($action1){
// if files have been uploaded, process them ?>
<html>
<head>
<title>File Upload Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> <body bgcolor="#FFFFFF" text="#000000">
<p><font face="Arial, Helvetica, sans-serif"><font size="+1">File
Upload Results</font><br><br> <?
set_time_limit(60);
// make reasonably sure the script does not time out on large files
$path1 = dirname($PATH_TRANSLATED)."/upload/";
//print $PATH_TRANSLATED;
//print $path1."<br>";
// assign our path in a form PHP for Windows understands for($i=1;$i<3;$i++)
{
$temp1="file".$i;
$temp2="file".$i."_name";
$source=$$temp1;
$source_name=$$temp2;
// print $temp."=".$$temp."<br>"; //print $file1_name;
//print $file2_name;
//exit; //*****************
//$source = $file1;
//$source_name = $file1_name;
print $source."<br>";
print $source_name;
//exit;
if(($source <> "none")&&($source <> ""))
{
// see if the file exists; non-existing file has name of "none"
if($error1 <> 1)
{
// no need to copy a file if directory not write-enabled
$dest = $path1.$source_name;
// append file name to our path if(copy($source,$dest)){
// copy the file from the temp directory to the upload directory, and test for success
echo "$source has been uploaded<br>\n";
$imagesize = getimagesize($dest);
switch($imagesize[2])
{
case 0:
echo "<br> Image is a unknown <br>";
unlink($dest);
exit;
case 1:
echo "<br> Image is a GIF <br>";
echo "$dest has a width of $imagesize[0]<br>";
echo "$dest has a height of $imagesize[1]<br>";
$newname = $path1;
$newname .=$i.time() . ".gif";
if(copy($dest,$newname))
{
echo "<br> GIF Rename Successful from $dest to $newname";
}else {
echo "<br> GIF Rename Unsuccessful";
}
unlink ($dest);
break;
case 2:
echo "<br> Image is a JPG <br>";
echo "$dest has a width of $imagesize[0]<br>";
echo "$dest has a height of $imagesize[1]<br>";
$newname = $path1;
$newname .=$i.time() . ".jpg";
if(copy($dest,$newname))
{
echo "<br> JPG Rename Successful from $dest to $newname";
}else {
echo "<br> JPG Rename Unsuccessful";
}
unlink ($dest);
break;
case 3:
echo "<br> Image is a PNG <br>";
echo "$dest has a width of $imagesize[0]<br>";
echo "$dest has a height of $imagesize[1]<br>";
$newname = $path1;
$newname .=time() . ".png";
if(copy($dest,$newname))
{
echo "<br> PNG Rename Successful from $dest to $newname";
}else {
echo "<br> PNG Rename Unsuccessful";
}
unlink ($dest);
break;
}
}else {
echo "Upload directory not write-enabled\n";
// you need to write-enable the upload directory
$error1 = 1; // set flag
}
}
unlink($source);
// delete the file from the temp directory
} ?>
<image src="upload/<? print basename($newname)?>">
<?
} //end for
?>
<br><a href="fup.php">Back</a>
</font></p>
</body>
</html> <?
}
else { ?> <html>
<head>
<title>File Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head> <body bgcolor="#FFFFFF" text="#000000">
<p><font face="Arial, Helvetica, sans-serif"><font size="+1">File
Upload</font><br><br>
If your browser is upload-enabled, you will see "Browse"
(Netscape, Internet Explorer) or ". . ." (Opera) buttons below.
Use them to select file(s) to upload, then click the "Upload"
button. After the files have been uploaded, you will see a results screen.<br>
<form method="post" enctype="multipart/form-data" action="fup.php">
<input type="hidden" name="MAX_FILE_SIZE" value="800000">
<input type="hidden" name="action1" value="1">
File 1: <input type="file" name="file1" size="30"><br>
File 2: <input type="file" name="file2" size="30"><br>
<br>
<input type="submit" value="Upload">
</form>
</font></p>
</body>
</html> <? } ?>
来源:奥索网
让你同时上传 1000 个文件 (一)
作者:gearsoft预备知识:Javascript, PHP(少量)/Perl(少量)!
测试环境:Windows2000 + IIS5 + PHP (Win98 + PWS + PHP3 失败,估计是配置出了问题)
目录结构:
/wwwroot/cgi-bin/fileup.php (文件接收)
/wwwroot/www/test/phpfileup.htm (文件提交)
/wwwroot/www/test/tmp/ (默认保存目录) 前言:文件上传,简单又麻烦。下面是上传文件的提交页面,利用该页面你不仅可以生成 1000 个上传文件框(实际是任意多个 0~n ),而且可分别指出它们的保存路径。
提交页面的文件输入框为命名为: file0,file1,...file100,...fileN
提交页面的文件路径框为命名为: path0,path1,...path100,...pathN
由于页面的生成非常简单,所以就不在此多解释了,用 javascript 定义了两个函数,check() 用于提交页面,create()用于生成文件上传框。如果你有什么更好的建议或有什么疑问请 Email: [email protected] phpfileup.htm
--------------------------------------------------------
<html>
<head>
<title></title>
<style>
.lbtn {font-family:verdana;font-size:10.5pt;}
.ist {font-family:verdana;font-size:14.8px;size:400}
</style> <script LANGUAGE="javascript">
function check(){
document.frm1.submit();
document.body.innerHTML="Uploading Please wait!";
} var i=0;
function create(){
var sfrm = document.frm1.innerHTML;
var icnt = cnt.value;
for(j=0;j<icnt;j++)
{
sfrm = sfrm + "请选择文件 "+i+" <input type=file name='file"+i+"' class=ist>";
sfrm = sfrm + " 路径:<input type=text name='path"+i+"' value='../www/test/tmp/' class=ist>";
sfrm = sfrm + "<br>";
i++;
}
document.frm1.innerHTML = sfrm;
document.frm1.cnt.value=i;
} </script> </head>
<body class=lbtn onload="document.frm1.cnt.value=cnt.value;">
请输入要上传文件的数量: <input type=text name=cnt value="10" class=ist onchange="document.frm1.cnt.value=this.value;">
<input type=button name=bt1 value="生成上传文件框" onclick="create();" class=lbtn>
<input type=button name=bt1 value="上传" onclick="check();" class=lbtn>
<input type=button name=bt1 value="清除" onclick="document.location.reload();" class=lbtn> <form name=frm1 method="post" Enctype="multipart/form-data" action="../../cgi-bin/fileup.php">
<input type=hidden name=cnt value="20" class=ist>
</form> <input type=button name=bt1 value="上传" onclick="check();" class=lbtn>
<input type=button name=bt1 value="清除" onclick="document.location.reload();" class=lbtn>
</body>
</html>
-------------------------------------------------------------------
(待续)
让你同时上传 1000 个文件(二)
来源:奥索网
让你同时上传 1000 个文件 (二)
作者:gearsoft 文件提交页面既已生成,下面任务就很明确了:将提交的文件内容保存到服务器上。 下面我们用两种方法来实现这个功能: 1. 用 PHP 来保存:
我们先定义一个文件保存函数 fup() 它有两个参数:
$filename: 文件内容
$fname: 文件名(包含路径)
剩下的就是写一个循环将文件依次写入服务器。这里要简单说明一下:
PHP 对于上传文件的处理是这样的:如果提交的文件框我为 file0, 那么提交给 PHP 的文件内容保存在变量 $file0 中,而文件名则保存在 $file0_name 中。这样在这个循环中我要做的就是将提交页面提交的内容分解出来,实现过程请看下面的代码。 fileup.php
----------------------------------------------------------------------
<?
function fup($filename,$fname)
{ If($filename != "none") {
copy($filename,$fname);
unlink($filename);
}
} for($i=0;$i<$cnt;$i++)
{
$ffnn="file".$i;
$ffnnname=$ffnn."_name";
$ffpath="path".$i; //print $$ffnn;
print $$ffnnname;
print "<br>"; fup($$ffnn,$$ffpath.$$ffnnname); //"../www/test/tmp/"
}
?>
---------------------------------------------------------------------- 2. 用 PERL 来保存:
它们实现的原理完全一样,在此不多说,请看代码: fileup.cgi(fileup.pl)
----------------------------------------------------------------------
#!/usr/bin/perl use CGI qw/:standard/; if ($ENV{'CONTENT_TYPE'} !~ /multipart/form-data/) {
print "Cache-Control: no-cachenPragma: no-cachen" .
"Content-Type: text/htmlnn" .
"<html><body>Your web browser cannot upload files. Sorry.</body></html>";
exit 0;
}
$cntfile=param('cnt');
print header;
print start_html;
#print "Receiving Please wait...."; &g_head; #$writed = '../www/test/tmp/'; for ($i=0;$i<$cntfile;$i++){
$paramfile = 'file'.$i;
$parampath='path'.$i; $writed=param($parampath); &upfile;
&g_body;
} &g_bott; #<<<<<<<<<<<<<<<<<<<<<以下为自定义过程<<<<<<<<<<<<<<<<<<<<<<<<< sub upfile
{
$maxdata = 512000;
# $writed = '../www/test/tmp/';
$strRFname=reverse $xfile;
$intIndex=index($strRFname,'\');
$strNetFname=substr($strRFname,0,$intIndex);
$strNetFname=scalar reverse $strNetFname;
if((stat $xfile)[7]>$maxdata){
print "Status: 411 Size Not Allowedn" .
"Content-Type: text/htmlnAllow: POSTnn" .
"<html><head><title>411 411 Size Not Allowed</title></head><body><h1> You got big problem. Try again.</h1></body></html>n";
exit 0;
}
binmode $xfile;
use File::Copy;
copy($xfile,$writed.$strNetFname);
} sub g_head{
print '<table border=1 align=center>';
print '<tr><td colspan=3 align=center>文件上传结果(Upload Result)</td></tr>';
print '<tr align=center>';
print ' <td>SourceFile:</td>';
print ' <td>DestFile:</td>';
print ' <td>Upload</td>';
print '</tr>';
} sub g_body{
print '<tr>';
print ' <td>'.$xfile .'</td>';
print ' <td>'.$writed.$strNetFname.'</td>';
print ' <td>OK!</td>';
print '</tr>';
} sub g_bott{
print '</table>';
}
----------------------------------------------------------------------
如有好的建议请 Email: [email protected]
可是问题在于我不上传很多文件,只上传一个大文件,例如650M,或者说今天我想上传10M,明天再上传20M,恐怕就得用socket了吧,我在vc中问了一些关于socket做ftp的问题,kevincheng给了我vc的例子,我也用VC做出了简单的程序。可是我发现将VC的原理应用到PHP上是如此的费劲(当然不排除我功力不够),在PHP中无法设置bind,listen等等,所以想就此类问题向热衷于php的兄弟们求助。
用Socket发送电子邮件
2000年09月01
作者/来源:limodou 用Socket发送电子邮件作者:limodou 在作者所申请的几个PHP 主页空间中,能够提供mail功能的实在不多,总是调用完mail()函数之后就毫
无下文了。但是电子邮件在网上生活中的作用越来越大。想一想网虫上网不收邮件能叫真正的网虫吗?邮件
的作用我不想再说了,但是如果主页空间不支持mail()发送那么怎么办呢?我也想过通过socket来实现邮件
发送,但无奈对用php 进行socket编程不熟悉,再加上发送邮件要用到SMTP协议,又要读不少的英文了,所
以一直也没有去研究过。终于有一天我发现了一篇文章,关于用socket编程发送邮件。我如获至宝般将其拷
贝下来,并且将其改造成了一个php 可用的类,供大家使用。原来的文章只是一个简单的例子,而且还有一
些错误,在我经过多次的实验、改造终于将其改成了一个直接使用socket,向指定的邮箱发送邮件的类,如
果大家和前面关于发送MIME的文章结合起来,就可以实现在不支持mail()函数的网站上发送邮件了。因为发
送邮件的过程需要时间,可能与mail()的处理机制还不完全一样,所以速度要慢一些,但是可以解决需要发
送邮件功能的燃眉之急,同时你也可以学习用php 进行socket编程。下面就将这个类的实现原理介绍给大家,
同时向大家讲解一些关于SMTP的基本知识。 Socket编程介绍
向大家申明,本人不是一个TCP/IP编程专家,故在此只是讲出了我的一点理解和体会。 使用fsockopen函数打开一个Internet连接,函数语法格式: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]); 参数的意思我想不用讲了,这里由于要使用SMTP协议,所以端口号为25。在打开连接成功后,会返回一
个socket句柄,使用它就可以象使用文件句柄一样的。可使用的操作有fputs(),fgets(),feof(),fclose()
等。 很简单地介绍就到这里吧。 SMTP的基础
基于TCP/IP的因特网协议一般的命令格式都是通过请求/ 应答方式实现的,采用的都是文本信息,所以
处理起来要容易一些。SMTP是简单邮件传输协议的简称,它可以实现客户端向服务器发送邮件的功能。所以
下面所讲的命令是指客户端向服务器发出请求指令,而响应则是指服务器返回给客户端的信息。 SMTP分为命令头和信息体两部分。命令头主要完成客户端与服务器的连接,验证等。整个过程由多条命
令组成。每个命令发到服务器后,由服务器给出响应信息,一般为3 位数字的响应码和响应文本。不同的服
务器返回的响应码是遵守协议的,但是响应正文本则不必。每个命令及响应的最后都有一个回车符,这样使
用fputs()和fgets()就可以进行命令与响应的处理了。SMTP的命令及响应信息都是单行的。信息体则是邮件
的正文部分,最后的结束行应以单独的"."作为结束行。 客户端一些常用的SMTP指令为: HELO hostname: 与服务器打招呼并告知客户端使用的机器名字,可以随便填写
MAIL FROM: sender_id : 告诉服务器发信人的地址
RCPT TO: receiver_id : 告诉服务器收信人的地址
DATA : 下面开始传输信件内容,且最后要以只含有.的特殊行结束
RESET: 取消刚才的指令,从新开始
VERIFY userid: 校验帐号是否存在(此指令为可选指令,服务器可能不支持)
QUIT : 退出连接,结束
服务器返回的响应信息为(格式为:响应码+空格+解释): 220 服务就绪(在socket连接成功时,会返回此信息)
221 正在处理
250 请求邮件动作正确,完成(HELO,MAIL FROM,RCPT TO,QUIT指令执行成功会返回此信息)
354 开始发送数据,结束以 .(DATA指令执行成功会返回此信息,客户端应发送信息)
500 语法错误,命令不能识别
550 命令不能执行,邮箱无效
552 中断处理:用户超出文件空间
下面给出一个简单的命令头(这是在打开socket之后做的),是我向stmp.263.net发邮件的测试结果: HELO limodou
250 smtp.263.net
MAIL FROM: [email protected]
250 Ok
RCPT TO: [email protected]
250 Ok
DATA
354 End data with .
To: [email protected]
From: [email protected]
Subject: test
From: [email protected]
test
.
QUIT
250 Ok: queued as C46411C5097E0 这就是一些SMTP的简单知识。相关内容可以查阅RFC。 RFC 821定义了收/发电子邮件的相关指令。
RFC 822则制定了邮件內容的格式。
RFC 2045-2048制定了多媒体邮件內容的格式,
RFC 1113, 1422-1424则是讨论如何增进电子邮件的保密性。 send_mail类的实现
现在开始介绍我所编写的发送邮件类。有了上面的预备知识了,下面就是实现了。 类的成员变量 var $lastmessage; //记录最后返回的响应信息
var $lastact; //最后的动作,字符串形式
var $welcome; //用在HELO后面,欢迎用户
var $debug; //是否显示调试信息
var $smtp; //smtp服务器
var $port; //smtp端口号
var $fp; //socket句柄 其中,$lastmessage和$lastact用于记录最后一次响应信息及执行的命令,当出错时,用户可以使用它
们。为了测试需要,我还定义了$debug变量,当其值为true时,会在运行过程中显示一些执行信息,否则无
任何输出。$fp用于保存打开后的socket句柄。 类的构造
--------------------------------------------------------------------------------
function send_mail($smtp, $welcome="", $debug=false)
{
if(empty($smtp)) die("SMTP cannt be NULL!");
$this->smtp=$smtp;
if(empty($welcome))
{
$this->welcome=gethostbyaddr("localhost");
}
else
$this->welcome=$welcome;
$this->debug=$debug;
$this->lastmessage="";
$this->lastact="";
$this->port="25";
}
--------------------------------------------------------------------------------
这个构造函数主要完成一些初始值的判定及设置。$welcome用于HELO指令中,告诉服务器用户的名字。
HELO指令要求为机器名,但是不用也可以。如果用户没有给出$welcome,则自动查找本地的机器名。 显示调试信息 --------------------------------------------------------------------------------
1 function show_debug($message, $inout)
2 {
3 if ($this->debug)
4 {
5 if($inout=="in") //响应信息
6 {
7 $m='<< ';
8 }
9 else
10 $m='>> ';
11 if(!ereg(" $", $message))
12 $message .= "<br>";
13 $message=nl2br($message);
14 echo "<font color=#999999>${m}${message}</font>";
15 }
16 }
--------------------------------------------------------------------------------
这个函数用来显示调试信息。可以在$inout中指定是上传的指令还是返回的响应,如果为上传指令,则
使用"out";如果为返回的响应则使用"in"。 第3行,判断是否要输出调试信息。
第5行,判断是否为响应信息,如果是,则在第7行将信息的前面加上"<< "来区别信息;否则在第10行加上
">> "来区别上传指令。
第11-12行,判断信息串最后是否为换行符,如不是则加上HTML换行标记。第13行将所以的换行符转成HTML
的换行标记。
第14行,输出整条信息,同时将信息颜色置为灰色以示区别。 执行一个命令
--------------------------------------------------------------------------------
1 function do_command($command, $code)
2 {
3 $this->lastact=$command;
4 $this->show_debug($this->lastact, "out");
5 fputs ( $this->fp, $this->lastact );
6 $this->lastmessage = fgets ( $this->fp, 512 );
7 $this->show_debug($this->lastmessage, "in");
8 if(!ereg("^$code", $this->lastmessage))
9 {
10 return false;
11 }
12 else
13 return true;
14 }
--------------------------------------------------------------------------------
在编写socket处理部分发现,一些命令的处理很相似,如HELO,MAIL FROM,RCPT TO,QUIT,DATA命令,
都要求根据是否显示调试信息将相关内容显示出来,同时对于返回的响应码,如果是期望的,则应继续处理,
如果不是期望的,则应中断出理。所以为了清晰与简化,专门对这些命令的处理编写了一个通用处理函数。
函数的参数中$code为期望的响应码,如果响应码与之相同则表示处理成功,否则出错。 第3行,记录最后执行命令。
第4行,将上传命令显示出来。
第5行,则使用fputs真正向服务器传换指令。
第6行,从服务器接收响应信息将放在最后响应消息变量中。
第7行,将响应信息显示出来。
第8行,判断响应信息是否期待的,如果是则第13行返回成功(true),否则在第10行返回失败(false)。 这样,这个函数一方面完成指令及信息的发送显示功能,别一方面对返回的响应判断是否成功。 邮件发送处理 下面是真正的秘密了,可要看仔细了。:) --------------------------------------------------------------------------------
1 function send( $to,$from,$subject,$message)
2 {
3
4 //连接服务器
5 $this->lastact="connect";
6
7 $this->show_debug("Connect to SMTP server : ".$this->smtp, "out");
8 $this->fp = fsockopen ( $this->smtp, $this->port );
9 if ( $this->fp )
10 {
11
12 set_socket_blocking( $this->fp, true );
13 $this->lastmessage=fgets($this->fp,512);
14 $this->show_debug($this->lastmessage, "in");
15
16 if (! ereg ( "^220", $this->lastmessage ) )
17 {
18 return false;
19 }
20 else
21 {
22 $this->lastact="HELO " . $this->welcome . " ";
23 if(!$this->do_command($this->lastact, "250"))
24 {
25 fclose($this->fp);
26 return false;
27 }
28
29 $this->lastact="MAIL FROM: $from" . " ";
30 if(!$this->do_command($this->lastact, "250"))
31 {
32 fclose($this->fp);
33 return false;
34 }
35
36 $this->lastact="RCPT TO: $to" . " ";
37 if(!$this->do_command($this->lastact, "250"))
38 {
39 fclose($this->fp);
40 return false;
41 }
42
43 //发送正文
44 $this->lastact="DATA ";
45 if(!$this->do_command($this->lastact, "354"))
46 {
47 fclose($this->fp);
48 return false;
49 }
50
51 //处理Subject头
52 $head="Subject: $subject ";
53 if(!empty($subject) && !ereg($head, $message))
54 {
55 $message = $head.$message;
56 }
57
58 //处理From头
59 $head="From: $from ";
60 if(!empty($from) && !ereg($head, $message))
61 {
62 $message = $head.$message;
63 }
64
65 //处理To头
66 $head="To: $to ";
67 if(!empty($to) && !ereg($head, $message))
68 {
69 $message = $head.$message;
70 }
71
72 //加上结束串
73 if(!ereg(" . ", $message))
74 $message .= " . ";
75 $this->show_debug($message, "out");
76 fputs($this->fp, $message);
77
78 $this->lastact="QUIT ";
79 if(!$this->do_command($this->lastact, "250"))
80 {
81 fclose($this->fp);
82 return false;
83 }
84 }
85 return true;
86 }
87 else
88 {
89 $this->show_debug("Connect failed!", "in");
90 return false;
91 }
92 }
--------------------------------------------------------------------------------
有些意思很清楚的我就不说了。 这个函数一共有四个参数,分别是$to表示收信人,$from表示发信人,$subject表求邮件主题和$message
表示邮件体。如果处理成功则返回true,失败则返回false。 第8行,连接邮件服务器,如果成功响应码应为220。
第12行,设置阻塞模式,表示信息必须返回才能继续。详细说明看手册吧。
第16行,判断响应码是否为220,如果是,则继续处理,否则出错返回。
第22-27行,处理HELO指令,期望响应码为250。
第29-34行,处理MAIL FROM指令,期望响应码为250。
第36-41行,处理RCPT TO指令,期望响应码为250。
第44-49行,处理DATA指令,期望响应码为354。
第51-76行,生成邮件体,并发送。
第52-56行,如果$subject不为空,则查找邮件体中是否有主题部分,如果没有,则加上主题部分。
第59-63行,如果$from不为空,则查找邮件体中是否有发信人部分,如果没有,则加上发信人部分。
第66-70行,如果$to不为空,则查找邮件体中是否有收信人部分,如果没有,则加上收信人部分。
第73-74行,查找邮件体是否有了结束行,如果没有则加上邮件体的结束行(以"."作为单独的一行的特殊行)。
第76行,发送邮件体。
第78-83行,执行QUIT结否与服务器的连接,期望响应码为250。
第85行,返回处理成功标志(true)。
第81-91行,与服务器连接失败的处理。 以上为整个send_mail类的实现,应该不是很难的。下面给出一个实例。 邮件发送实例
先给出一个最简单的实例:
--------------------------------------------------------------------------------
<?
1 include "sendmail.class.php3";
2 $email="Hello, this is a test letter!";
3 $sendmail=new send_mail("smtp.263.net", "limodou", true); //显示调示信息
4 if($sendmail->send("[email protected]", "[email protected]", "test", $email))
5 {
6 echo "发送成功!<br>";
7 }
8 else
9 {
10 echo "发送失败!<br>";
11 }
?>
--------------------------------------------------------------------------------
第1行,装入send_mail类。
第3行,创建一个类的实例,且设置显示调示信息,如果不想显示,可以
$sendmail=new send_mail("smtp.263.net");。
第4行,发送邮件。
很简单,不是吗?下面再给合以前的发送MIME邮件的例子,给出一个发送HTML附件的例子。 --------------------------------------------------------------------------------
<?php include "MIME.class.php3";
//注,在发送MIME邮件一文中,这个类文件名为MIME.class,在此处我改成这样的 $to = '[email protected]'; //改为收信人的邮箱
$str = "Newsletter for ".date('M Y', time()); //信息被我改少了
$html_data = '<html><head><title>'. $str. '</title></head>
<body bgcolor="#ffffff">
Hello! This is a test!
</body>
</html>'; //生成MIME类实例
$mime = new MIME_mail("[email protected]", $to, $str); //添加HTML附件
$mime->attach($html_data, "", HTML, BASE64); //注释掉,采用我的发送邮件处理
//$mime->send_mail(); //生成邮件
$mime->gen_email(); //显示邮件信息
//echo $mime->email."<br>"; //包含sendmail文件
include "sendmail.class.php3"; //创建实例
$sendmail=new send_mail("smtp.263.net", "limodou", true); //发送邮件
$sendmail->send("[email protected]", "[email protected]", $str, $mime->email); ?>
--------------------------------------------------------------------------------
注释写的很清楚,就不再做更多的解释了。如果实际应用中,请将send_mail构造函数中的debug设为
false或不写即可。在此处可以下载关于本文的例子。
--------------------------------
<?php
$fp = fsockopen("wg", 21, &$errno, &$errstr, 10);
if(!$fp) {
echo "$errstr ($errno)<br>\n";
} else { $aa= fgets($fp,256);
echo "$aa<br>"; fputs($fp,"USER wg1\r\n");
echo fgets($fp,256);
echo "<br>"; $ret=fputs($fp,"PASS \r\n");
echo fgets($fp,256);
echo "<br>"; $ret=fputs($fp,"TYPE I\r\n");
echo fgets($fp,256);
echo "<br>"; $ret=fputs($fp,"REST 100\r\n");
echo fgets($fp,256);
echo "<br>"; $ret=fputs($fp,"REST 0\r\n");
echo fgets($fp,256);
echo "<br>"; $ret=fputs($fp,"pwd\r\n");
echo fgets($fp,256);
echo "<br>"; $ret=fputs($fp,"TYPE A\r\n");
echo fgets($fp,256);
echo "<br>";
$ret=fputs($fp,"retr wgnote.txt\r\n");
echo fgets($fp,256);
echo "<br>"; echo fgets($fp,256);
echo "<br>"; $ret=fclose($fp);
}
?>
------------------
结果如下:
------------------------
220 wg Microsoft FTP Service (Version 5.0).
331 Password required for wg1.
230-Hello,This is Wg's FTP Server
230 User wg1 logged in.
200 Type set to I.
350 Restarting at 100.
350 Restarting at 0.
257 "/" is current directory.
200 Type set to A.
150 Opening ASCII mode data connection for wgnote.txt(18538 bytes).
------------------------------------
retr命令好象执行的正确,但是无法得到从服务器返回的信息,我应该如何在php中得到retr的文件流或句柄呢?
访问http://168.168.18.11:81/etechbase/advsearch.php将您的问题输入查询内容框,选择不同的精确程度,即可以找到你所需要的答案。效果还是可以的。