Category Archives: Uncategorized

Handling interrupted file uploads

There are occasionally problems involving parsing the uploaded file. This usually happens when the user presses “Stop” before the upload is finished. In this case, CGI.pm will return undef for the name of the uploaded file and set cgi_error() to the string “400 Bad request (malformed multipart POST)”. This error message is designed so that you can incorporate it into a status code to be sent to the browser. Example:

$file = $q->upload(‘uploaded_file’);
if (!$file && $q->cgi_error) {
print $q->header(-status=>$q->cgi_error);
exit 0;
}

You are free to create a custom HTML page to complain about the error, if you wish.

Accessing the temp files directly in perl

When processing an uploaded file, CGI.pm creates a temporary file on your hard disk and passes you a file handle to that file. After you are finished with the file handle, CGI.pm unlinks (deletes) the temporary file. If you need to you can access the temporary file directly. You can access the temp file for a file upload by passing the file name to the tmpFileName() method:

$filename = $query->param(‘uploaded_file’);
$tmpfilename = $query->tmpFileName($filename);

The temporary file will be deleted automatically when your program exits unless you manually rename it. On some operating systems (such as Windows NT), you will need to close the temporary file’s filehandle before your program exits. Otherwise the attempt to delete the temporary file will fail.

File type in uploading content perl

$filename = $q->param(‘uploaded_file’);
$type = $q->uploadInfo($filename)->{‘Content-Type’};
unless ($type eq ‘text/html’) {
die “HTML FILES ONLY!”;
}

PROCESSING A FILE UPLOAD FIELD IN PERL

Basics

When the form is processed, you can retrieve an IO::Handle compatible handle for a file upload field like this:

$lightweight_fh = $q->upload(‘field_name’);
# undef may be returned if it’s not a valid file handle
if (defined $lightweight_fh) {
# Upgrade the handle to one compatible with IO::Handle:
my $io_handle = $lightweight_fh->handle;
open (OUTFILE,’>>’,’/usr/local/web/users/feedback’);
while ($bytesread = $io_handle->read($buffer,1024)) {
print OUTFILE $buffer;
}
}

In a list context, upload() will return an array of filehandles. This makes it possible to process forms that use the same name for multiple upload fields.

If you want the entered file name for the file, you can just call param():

$filename = $q->param(‘field_name’);

Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user’s machine. Regardless, the name returned is always the name of the file on the user’s machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

When a file is uploaded the browser usually sends along some information along with it in the format of headers. The information usually includes the MIME content type. To retrieve this information, call uploadInfo(). It returns a reference to a hash containing all the document headers.

$filename = $q->param(‘uploaded_file’);
$type = $q->uploadInfo($filename)->{‘Content-Type’};
unless ($type eq ‘text/html’) {
die “HTML FILES ONLY!”;
}

If you are using a machine that recognizes “text” and “binary” data modes, be sure to understand when and how to use them (see the Camel book). Otherwise you may find that binary files are corrupted during file uploads.

Jquery in PHP & Perl…

When we try to print “jquery” inside “PHP or Perl” using “echo or print” statement we should escape the$” symbol used in the jquery. It should be as follows.

print qq{

<script type=”text/javascript”>
(function(\$) {
\$(function() {
\$(“#scroller”).simplyScroll({orientation:’vertical’,customClass:’vert’});
});
})
(jQuery);
(function(\$) {
\$(function() {
\$(“#scroller1”).simplyScroll({direction:’backwards’});
});
})(jQuery);
(function(\$) {
\$(function() {
\$(“#scroller2”).simplyScroll({direction:’backwards’});
});
})(jQuery);
</script>

}

echo ‘

<script type=”text/javascript”>
(function(\$) {
\$(function() {
\$(“#scroller”).simplyScroll({orientation:’vertical’,customClass:’vert’});
});
})
(jQuery);
(function(\$) {
\$(function() {
\$(“#scroller1”).simplyScroll({direction:’backwards’});
});
})(jQuery);
(function(\$) {
\$(function() {
\$(“#scroller2”).simplyScroll({direction:’backwards’});
});
})(jQuery);
</script>

‘;