网页文件上传总是损坏 [英] Web page file upload is always corrupted

查看:155
本文介绍了网页文件上传总是损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传添加到现有网页。

I am trying to add a file upload to an existing web page.

每次上传时,我都会收到损坏的文件。

Every time I upload I get a file that is corrupted.

我确保在文件句柄上设置 binmode 。我也将表单中的输入 enctype 设置为 multipart / formdata

I made sure to set binmode on the file handle. I also have my input enctype set to multipart/formdata in my form.

我的代码如下

$article{upload_file_name} = $cgi->param( 'upFile' );
$article{upload_file}      = $cgi->upload( 'upFile' );

if ( $article{upload_file_name} ne "" or $article{upload_file_name} ne null ) {

    open( UPLOADS, ">$uploads_dir/$article{upload_file_name}" )
            or die "Could not open $uploads_dir/$article{upload_file_name}";

    binmode UPLOADS;

    while ( <$article{upload_file}> ) {
        print UPLOADS;
    }

    close UPLOADS;
}

我也尝试过此操作

$article{upload_file} = $cgi->param( 'upFile' );

if ( $article{upload_file} ne "" or $article{upload_file} ne null ) {

    open( UPLOADS, ">$uploads_dir/$article{upload_file}" )
            or die "Could not open $uploads_dir/$article{upload_file}";
    binmode UPLOADS;

    while ( <$article{upload_file}> ) {
        print UPLOADS;
    }

    close UPLOADS;
}


推荐答案

<$article{upload_file}>

没有按照您认为的去做。钻石运算符(<> )用于代表Perl的 readline 函数,但它的第二个含义是它与Perl的 glob 函数。在Perl的解析规则中,< $ hash {key}> 始终被视为 glob

is not doing what you think it is doing. The diamond operator (<>) is used to stand in for Perl's readline function, but it has a second meaning where it does the same thing as Perl's glob function. And in Perl's parsing rules, <$hash{key}> is always treated as glob.

perldoc- f perlop 解释:


如果尖括号中的内容既不是文件句柄也不是简单的包含文件句柄名称,typeglob或typeglob引用的标量变量,它被解释为要被遍历的文件名模式,并根据上下文返回文件名列表或列表中的下一个文件名。这种区别仅基于句法基础来确定。这意味着< $ x>始终是来自间接句柄的readline(),但< $ hash {key}>始终是glob()。这是因为$ x是简单的标量变量,而$ hash {key}不是-它是一个哈希元素。甚至< $ x> (请注意多余的空间)被视为glob( $ x),而不是readline($ x)。

If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context. This distinction is determined on syntactic grounds alone. That means <$x> is always a readline() from an indirect handle, but <$hash{key}> is always a glob(). That's because $x is a simple scalar variable, but $hash{key} is not--it's a hash element. Even <$x > (note the extra space) is treated as glob("$x "), not readline($x).

至少有两种解决方法:


  1. 使用明确的 readline 调用:

while (readline($article{upload_file}))


  • 将文件句柄分配给简单标量

  • Assign filehandle to a simple scalar

    my $upload_fh = $article{upload_file};
    ...
    while (<$upload_fh>)
    


  • 这篇关于网页文件上传总是损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆