In fixing a new bug recently, I stumbled upon an odd behaviour. When I upload a file via an HTML page, retrieve the file name using CGI, and then call decode_entities on the file name, the result is an empty string. This seems to be Perl version dependent: It fails on Perl v5.20.2 but doesn't fail on v5.10.1. Here is a simple test case:

HTML page:

<DOCTYPE html> <HTML> <BODY> <form action="filename_entity_test.cgi" method="post" enctype="multipa +rt/form-data"> Test file name: <input type="file" name="fn"> <br> <input type="submit" name="submit" value="Upload"> </form> </BODY> </HTML>

CGI code:

#!/usr/bin/perl -w use strict; use CGI qw(:cgi-lib); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use HTML::Entities; my ($cg, $filename); $cg = new CGI; print $cg->header; print $cg->start_html("Testing decode_entities"); $filename = $cg->param("fn"); print "Filename before call to decode_entities: $filename<br>\n"; decode_entities($filename); print "Filename after call to decode_entities: $filename<br>\n"; print $cg->end_html;

Calling decode_entities with other cgi parameters is successful, and calling it like this:

$filename = decode_entities($filename);
is also successful.

In retrospect, I don't really need to worry about entities in the filename, but I'm very curious as to what is going on here.


In reply to CGI Filenames and decode_entities by Marais

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.