免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4333 | 回复: 6
打印 上一主题 下一主题

为什么我上传文件后,变成0字节了呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-30 10:45 |只看该作者 |倒序浏览
不太明白,请各位帮忙!
不知道哪里有问题,上传的文件都是0字节的
  1. #!/usr/bin/perl -w

  2. print "content-type: text/html\n\n";
  3.         use strict;
  4.         use CGI qw(:standard);

  5. print header;
  6. print start_html(-title=>'test upload file');
  7. print start_form(-method=>'POST',
  8.                  -enctype=>"multipart/form-data",
  9.                  -action=>'/awm/dd.pl');
  10. print <<EOF;
  11.         <input type="file" name="uploadfile">
  12. EOF

  13. ##print start_multipart_form.filefield(-name=>'uploadfile');
  14. print br.submit('submit','Send').end_form;
  15. print end_html;

  16. my $upload_dir = "/usr/local/aaa/uploadfile" ;
  17. if(param()){
  18.         my $q = new CGI;
  19.         my $filename = $q->param('uploadfile');
  20.         $filename =~ s/.*[\/\\](.*)/$1/;
  21.         print $filename."<br>";
  22.         open UPLOAD, ">$upload_dir/$filename";
  23.         binmod UPLOAD;
  24.         close UPLOAD;
  25. }
复制代码

[ 本帖最后由 justinyun 于 2007-9-30 11:10 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-09-30 11:19 |只看该作者
看看权限设置是否正确吧

论坛徽章:
0
3 [报告]
发表于 2007-10-05 07:35 |只看该作者
你都不读一下不写一下怎么把文件写进去啊
http://search.cpan.org/~lds/CGI.pm-3.29/CGI.pm

论坛徽章:
0
4 [报告]
发表于 2007-10-08 10:29 |只看该作者
去掉binmode,然后加上写,如下:
while (my $bytesread = read($file, my $buffer, 1024)) {
                print OUTFILE $buffer;
        }

之前要加上my $file = $q->param('uploadfile');

可是如果是加binmode ,就是以二进制上传,会报IO:handle的错,需要怎么做?

论坛徽章:
0
5 [报告]
发表于 2007-10-08 21:28 |只看该作者
之前要加上my $file = $q->param('uploadfile');

你用的$file是一个字符串,但是
  1. However, there are problems with the dual nature of the upload fields. If you use strict, then Perl will complain when you try to use a string as a filehandle. You can get around this by placing the file reading code in a block containing the no strict pragma. More seriously, it is possible for the remote user to type garbage into the upload field, in which case what you get from param() is not a filehandle at all, but a string.
复制代码

所以
  1. To be safe, use the upload() function (new in version 2.47). When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle.

  2.      $fh = upload('uploaded_file');
  3.      while (<$fh>) {
  4.            print;
  5.      }
复制代码

用upload来获得文件句柄好了

[ 本帖最后由 kamixp 于 2007-10-8 21:33 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-10-10 14:36 |只看该作者
嗯,两个都可以的,你的是直接while,而那上面那个有个read过程,

我想知道的是为什么二进制上传会报错,然后上传的文件就只是零字节,即没有写进去!
至于upload方式我也有试过,binmode就是会出错,不知道该怎么解决这个问题,我目前就只能把binmode给禁掉!

论坛徽章:
0
7 [报告]
发表于 2007-10-10 19:41 |只看该作者
  1. #!c:/perl/bin/perl.exe
  2. use CGI qw(:standard);
  3. print header;
  4. $fh = upload('uploaded_file');
  5. open HT,'>fukkk.txt' or die 'open file error';
  6.      while (<$fh>) {
  7.            print HT;
  8.      }
复制代码

你先用这个测,再改

不行再贴好代码和apache的error log

[ 本帖最后由 kamixp 于 2007-10-10 20:53 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP