LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1291|回复: 5

请问有哪位兄弟写过FTP服务器?

[复制链接]
发表于 2003-12-17 11:37:37 | 显示全部楼层 |阅读模式
因为个人毕业设计是一个电力系统的OA软件,要求能够实现使用浏览器能上传文件,并且文件大小没有限制,所以我就考虑做一个遵循FTP协议的小型服务器来实现,以Windows的服务形式运行,开发语言为Perl,哪位大哥开发过FTP服务器或者有相关的经验,请指点一二:)

另外有没有兄弟使用过Perl开发.dll的经验,请介绍一下撒:)
发表于 2003-12-18 14:18:50 | 显示全部楼层
I'm sorry ,一点都不懂。。:help :help
发表于 2003-12-18 20:08:24 | 显示全部楼层
我在"McGraw-Hill - Perl - The Complete Reference 2nd Ed"(這本書可用xmule下載)第二十二章中找到關於perl compiler 的應用, c/c++ 可以參考這個?韺?現的, 兄弟可以下?韰⒖
 楼主| 发表于 2003-12-18 21:11:34 | 显示全部楼层
谢谢两位:)
发表于 2003-12-19 20:56:51 | 显示全部楼层

从http://www.kehui.net/index.php找的

ftp  
        #!/usr/local/bin/perl  
#################################################################  
#  I  found  this  script  at  http://www.terminalp.com  
#  it  was  written  by  a  guy  named  Jeff  who  sold  the  domain  and  
#  disappeared.  I  deleted  the  original  header  to  save  space  
#  so  I  am  distributing  this  as  is.  If  Jeff  sees  this,  please  
#  email  me  at  dreun@eskimo.com!!  Thanks,  Ron  Hagerman  
#################################################################  

BEGIN  {  

$SAVE_DIRECTORY  =  "/your/literal/path/incoming/";  
#定义上载的文件的存放位置  
$MAXIMUM_UPLOAD  =  0;  
#最大上载数量  
$ALLOW_INDEX  =  0;  
是否允许上载文件名为index.*的文件  

$SUCCESS_LOCATION  =  "http://www.yourserver.com/~you/page.html";  
#定义当上载成功以后,显示该URL指向的内容  
}  

$|  =  1;  
#设定$OUTPUT_AUTOFLUSH为1,也就是使STDOUT不对输出进行缓冲,而是直接输出。  
chop  $SAVE_DIRECTORY  if  ($SAVE_DIRECTORY  =~  //$/);  
#若保存目的地址以"/"结尾  则将其删除  
use  CGI  qw(:standard);  
#使用CGI模块  
$query  =  new  CGI;  
#生成一个新的CGI对象  

#当指定的保存目的目录名不存在或不可写或不是目录时  输出错误信息  
if  (  (!(-e  $SAVE_DIRECTORY))  ||  
(!(-W  $SAVE_DIRECTORY))  ||  
(!(-d  $SAVE_DIRECTORY))  )  {  
print  header;  
#输出http信息头  
print  <<__END_OF_HTML_CODE__;  
#这里的<<__END_OF_HTML_CODE__指示print语句输出文件后面的内容  直到遇到__END_OF_HTML_CODE__  你可以使用别的符号来替代__END_OF_HTML_CODE__  
<HTML>  
<HEAD>  
<TITLE>Error:  Bad  Directory</TITLE>  
</HEAD>  
<BODY  BGcolor="#FFFFFF">  
<H1>Bad  Directory</H1>  
<>  
The  directory  you  specified:  
<BR>  
<BLOCKQUOTE>  
<TT>$SAVE_DIRECTORY  =  "<B>$SAVE_DIRECTORY</B>";</TT>  
</BLOCKQUOTE>  
<BR>  
is  invalid.  This  problem  is  caused  by  one  of  the  three  following  reasons:  
<OL>  
<LI>The  directory  doesn“t  exist.  Make  sure  that  this  directory  is  a  complete  path  name,  not  
a  URL  or  something  similar.  It  should  look  similar  to  <TT>/home/username/public_html/uploads</TT>  
<>  
<LI>The  directory  isn“t  writable.  Make  sure  that  this  directory  is  writable  by  all  users.  At  
your  UNIX  command  prompt,  type  <TT>chmod  777  $SAVE_DIRECTORY</TT>  
<>  
<LI>The  directory  you  specified  isn“t  really  a  directory.  Make  sure  that  this  is  indeed  a  directory  
and  not  a  file.  
</OL>  

</BODY>  
</HTML>  

__END_OF_HTML_CODE__  
exit;  
}  

foreach  $key  (sort  {$a  <=>  $b}  $query->param())  {  
#对html  form中的各个元素的名字进行排序  然后对每个名字进行如下操作  
next  if  ($key  =~  /^s*$/);  
#若名字为空  则进行下一次循环  
next  if  ($query->param($key)  =~  /^s*$/);  
#若名字对应的值为空  则进行下一次循环  
next  if  ($key  !~  /^file-to-upload-(d+)$/);  
#若名字不为file-to-upload-(数字)的形式  则进行下一次循环  
$Number  =  $1;  

if  ($query->param($key)  =~  /([^/]+)$/)  {  
#若取到的上载路径中的文件名部分不为空则  
$Filename  =  $1;  
$Filename  =~  s/^.+//;  
#将取到的文件名保存到变量$Filename中,并且去除文件名前的"."符号  
$File_Handle  =  $query->param($key);  
#完全路径的文件名保存到$File_Handle;  
if  (!$ALLOW_INDEX  &&  $Filename  =~  /^index/i)  {  
#若不允许上载文件名为index.*的文件而文件却恰恰为index.*形式则输出错误信息  
print  header;  
print  <<__END_OF_HTML_CODE__;  

<HTML>  
<HEAD>  
<TITLE>Error:  Filename  Problem</TITLE>  
</HEAD>  
<BODY  BGcolor="#FFFFFF">  
<H1>Filename  Problem</H1>  
<>  
You  attempted  to  upload  a  file  that  isn“t  properly  formatted.  The  system  administrator  
has  decided  that  you  can“t  upload  files  that  begin  with  the  word  “<B>index</B>“.  Please  
rename  the  file  on  your  computer,  and  try  uploading  it  again.  
<>  

</BODY>  
</HTML>  

__END_OF_HTML_CODE__  
exit;  
}  
#end  of  decide  whether  the  file  can  be  index.*  style  
}  else  {  
#当取得的文件名为空  则输出错误信息  
$FILENAME_IN_QUESTION  =  $query->param($key);  
#取得该文件路径到变量$FILENAME_IN_QUESTION中  然后输出错误信息  
print  header;  
print  <<__END_OF_HTML_CODE__;  

<HTML>  
<HEAD>  
<TITLE>Error:  Filename  Problem</TITLE>  
</HEAD>  
<BODY  BGcolor="#FFFFFF">  
<H1>Filename  Problem</H1>  
<>  
You  attempted  to  upload  a  file  that  isn“t  properly  formatted.  The  file  in  question  
is  <TT><B>$FILENAME_IN_QUESTION</B></TT>  Please  rename  the  file  on  your  computer,  and  
attempt  to  upload  it  again.  Files  may  not  have  forward  or  backward  slashes  in  their  
names.  Also,  they  may  not  be  prefixed  with  one  (or  more)  periods.  
<>  

</BODY>  
</HTML>  

__END_OF_HTML_CODE__  
exit;  
}  

if  (!open(OUTFILE,  ">$SAVE_DIRECTORY/$Filename"))  {  
#以写入的方式在上载目录下创建与上载文件同名的文件  若打开错误则输出错误信息  
print  "Content-type:  text/plainnn";  
print  "-------------------------n";  
print  "Error:n";  
print  "-------------------------n";  
print  "File:  $SAVE_DIRECTORY/$Filenamen";  
print  "-------------------------n";  
print  "There  was  an  error  opening  the  Output  Filen";  
print  "for  Writing.nn";  
print  "Make  sure  that  the  directory:n";  
print  "$SAVE_DIRECTORYn";  
print  "has  been  chmodded  with  the  permissions  “777“.nn";  
print  "Also,  make  sure  that  if  your  attemptingn";  
print  "to  overwrite  an  existing  file,  that  then";  
print  "existing  file  is  chmodded  “666“  or  better.nn";  
print  "The  Error  message  below  should  help  you  diagnosen";  
print  "the  problem.nn";  
print  "Error:  $!n";  
exit;  
}  

undef  $BytesRead;  
undef  $Buffer;  

while  ($Bytes  =  read($File_Handle,$Buffer,1024))  {  
#从要上载源文件读取1K的内容到变量buffer中  循环直到将文件内容全部读完  
$BytesRead  +=  $Bytes;  
#更新从该文件读取的字节数总数  
print  OUTFILE  $Buffer;  
#输出$buffer内容到目的文件中  
}  

push(@Files_Written,  "$SAVE_DIRECTORY/$Filename");  
#将上载的带有本机路径文件名加入到数组@Files_Written中  
$TOTAL_BYTES  +=  $BytesRead;  
#更新上载的数据量的总和  

$Confirmation{$File_Handle}  =  $BytesRead;  
#置关联数组值  {file_to_read  -->  file_to_read_length}  
close($File_Handle);  

close(OUTFILE);  
#关闭两个文件  

chmod  (0666,  "$SAVE_DIRECTORY/$Filename");  
#修改上载的文件访问权限位为666  
}  

$FILES_UPLOADED  =  scalar(keys(%Confirmation));  
#取得上载的文件数目  

if  ($TOTAL_BYTES  >  $MAXIMUM_UPLOAD  &&  $MAXIMUM_UPLOAD  >  0)  {  
#若上载的所有文件的大小总和大于变量$MAXIMUM_UPLOAD中规定的最大上载文件大小  
#且$MAXIMUM_UPLOAD不为0  则删除上载的文件  
foreach  $File  (@Files_Written)  {  
unlink  $File;  
}  
#打印错误信息  
print  header;  
print  <<__END_OF_HTML_CODE__;  

<HTML>  
<HEAD>  
<TITLE>Error:  Limit  Reached</TITLE>  
</HEAD>  
<BODY  BGcolor="#FFFFFF">  
<H1>Limit  Reached</H1>  
<>  
You  have  reached  your  upload  limit.  You  attempted  to  upload  <B>$FILES_UPLOADED</B>  files,  totalling  
<B>$TOTAL_BYTES</B>.  This  exceeds  the  maximum  limit  of  <B>$MAXIMUM_UPLOAD</B>  bytes,  set  by  the  system  
administrator.  <B>None</B>  of  your  files  were  successfully  saved.  Please  try  again.  
<>  

</BODY>  
</HTML>  

__END_OF_HTML_CODE__  
exit;  
}  

if  ($SUCCESS_LOCATION  !~  /^s*$/)  {  
print  $query->redirect($SUCCESS_LOCATION);  
#若定义的$SUCCESS_LOCATION不为空  则显示$SUCCESS_LOCATION指定的页面  
#否则输出信息  
}  else  {  


print  header;  
print  <<__END_OF_HTML_CODE__;  

<HTML>  
<HEAD>  
<TITLE>Upload  Finished</TITLE>  
</HEAD>  
<BODY  BGcolor="#FFFFFF">  
<H1>Upload  Finished</H1>  
<>  
You  uploaded  <B>$FILES_UPLOADED</B>  files  totalling  <B>$TOTAL_BYTES</B>  total  bytes.  Individual  
file  information  is  listed  below:  
<PRE>  

__END_OF_HTML_CODE__  

foreach  $key  (keys  (%Confirmation))  {  
print  "$key  -  $Confirmation{$key}  bytesn";  
}  

print  <<__END_OF_HTML_CODE__;  

</PRE>  
<P>  
Thank  you  for  using  the  File  Upload!  system.  
<P>  

</BODY>  
</HTML>  

__END_OF_HTML_CODE__  
exit;
 楼主| 发表于 2003-12-20 11:00:06 | 显示全部楼层
谢谢楼上的,我是想了解一下windows service程序的编写,这个应该是使用浏览器上传吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表