我正在尝试简单上传.csv文件并将其保存在我的服务器上。我不是HTML或Perl的专家,但由于客户限制,这是我必须做的。
这是HTML:
<form action="/path/to/service" target="_self" method="POST" enctype="multipart/form-data">
File: <input type="file" name="attachment" size="50">
<SUBMIT_RESET Upload File>
</form>
Perl代码如下所示:
my $sql = "SELECT NOW()";
my $date = $DB->get_field($sql);
my ($path, $ext) = split(/\./, $in{'attachment'});
my @pathParts = split(/\//, $path);
my $filename = $pathParts[@pathParts - 1] . " - " . $date;
if ($ext eq "csv") {
open (OUTFILE, ">", "$datadir/imports/" . $filename . "." . $ext);
while (read($in{'attachment'}, $buffer, 1024)) {
$file .= $buffer;
}
print OUTPUT $file;
close (OUTFILE);
}
任何人都可以给我一些关于我做错的方向。我把文件放在正确的路径上,但它总是空的。
现在,我在这里处理的代码库非常糟糕,我不能使用严格的。
建议?
EDIT1: 为了尝试回答如何填充{'attachment'}中的$,我已经包含了一段代码,用于表示如何处理表单。
if($ ENV {'CONTENT_TYPE'} = ~m#^ multipart / form-data#){
我的$ cgi =新的CGI;
我的@names = $ cgi-&gt; param;
foreach $name (@names) {
my @value = $cgi->param($name);
if (scalar(@value) > 1) {
foreach $val (@value) {
$in{$name} .= $val."\0";
}
$in{$name} =~s/\\0$//;
} else {
my $value = $cgi->param($name);
#my $value = join "\0", @value;
$in{$name} = $value;
}
#my $value = $cgi->param($name);
#my $value = join "\0", @value;
#$in{$name} = $value;
}
EDIT2:
总结ikegami提供的解决方案......
我错过了读取和打印语句的文件句柄不同 - OUTFILE
和OUTPUT
。显然这完全是一个新手的错误,为此我道歉!
祝所有试图帮助我的人。
答案 0 :(得分:3)
您不应该使用全局变量。如果你的文件句柄是词法变量(自2000年以来一直支持!),如果你一直在使用use strict;
(自2000年以来也支持,如果不是更早!),你会发现问题:
您未在print
电话中使用正确的变量。
open (OUTFILE, ">", "$datadir/imports/" . $filename . "." . $ext);
print OUTPUT $file;
当我发现问题时,我打算建议您找出实际上有什么错误:
open(my $OUTFILE, ">", "$datadir/imports/$filename.$ext")
or die $!;
while (1) {
my $rv = read($in{'attachment'}, my $buf, 1024);
die $! if !defined($rv);
last if !$rv;
print $OUTFILE $buf
or die $!;
}
close($OUTFILE)
or die $!;
(由于将不是文件句柄的内容传递给Bad file descriptor
,我假设错误为“read
”。)
答案 1 :(得分:0)
HTML代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Upload</title>
</head>
<body>
<form action="upload.cgi" method="post" enctype="multipart/form-data">
<p>Photo to Upload: <input type="file" name="photo" /></p>
<p><input type="submit" name="Submit" value="Submit Form" /></p>
</form>
</body>
</html>
文件名upload.cgi下的Perl Code :
#!C:\Program Files\perl\bin\perl.exe
use strict;
use CGI;
use CGI::Carp qw ( fatalsToBrowser );
use File::Basename;
$CGI::POST_MAX = 1024 * 5000;
my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "fileUpload/";
my $query = new CGI; my $filename = $query->param("photo");
if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}
my ( $name, $path, $extension ) = fileparse ( $filename, '..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/; $filename =~ s/[^$safe_filename_characters]//g;
if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
} else {
die "Filename contains invalid characters";
}
my $upload_filehandle = $query->upload("photo");
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );
print <<END_HTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thanks!</title>
<style type="text/css">
img {border: none;}
</style>
</head>
<body>
<p>Thanks for uploading your photo!</p>
</body>
</html>
END_HTML
我没有写这个代码,但它确实帮了我很多。我希望它对你也有帮助。使这段代码适合我的原因是让perl.exe的路径正确。