1
votes

I have the following problem: when I try to save the file that contains a semicolon in the name it returns a huge and weird stacktrace of the characters on the page. I've tried to escape, to trim and to replace those semicolons, but the result is still the same. I use the following regex:

$value =~ s/([^a-zA-Z0-9_\-.]|;)/uc sprintf("%%%02x",ord($1))/eg;

(I've even added the |; part separately..)

So, when I open the file to write and call the print function it returns lots of weird stuff, like that:

PK!}�3y�[Content_Types].xml ���/�h9\�?�0���cz��:� �s_����o���>�T�� (it is a huge one, this is just a part of it).

Is there any way I could avoid this?

Thank you in advance!

EDIT:

Just interested - what is the PK responsible of in this string? I mean I can understand that those chars are just contents of the file, but what is PK ? And why does it show the content type?

EDIT 2.0:

I'm uploading the .docx file - when the name doesn't contain the semicolon it works all fine. This is the code for the file saving:

open (QSTR,">", "$dest_file") or die "can't open output file: $qstring_file";
print QSTR $value;
close (QSTR);

EDIT 3.0

This is a .cgi script, that is called after posting some data to the server. It has to save some info about the uploading file to a temp file (name, contents, size) in the manner of key-value pairs. So any file that contains the semicolon causes this error.

EDIT 4.0 Found the cause:

The CGI param function while uploading the params counts semicolon as the delimiter! Is there any way to escape it in the file header?

1
Can you show us the code that you're using to save the file? I suspect that it's actually working properly, but the data that you're writing to the file simply isn't plain text - it's something like Postscript or PDF. I don't know what format the PK! marker actually indicates, but using your shell's file command might identify the format. In any case, I highly doubt that the filename is relevant to this. - Dave Sherohman
Sure. The edit is done. The file is not just text, you are right. - user
What about a bit more of context? You mention "the page". So is this some sort of web-app? Based on what framework? You say "I'm uploading the .docx file". What file? Uploading it where? And when? Does this trigger the error? What does PK mean? - innaM

1 Answers

1
votes

The PK in file header it means it is compressed ZIP like file, like docx.

One guess: The ; is not valid character in filename at the destination?

Your regexp is not good: (the dot alone is applicable to any character...)

$value =~ s/([^a-zA-Z0-9_\-.]|;)/uc sprintf("%%%02x",ord($1))/eg;

Try this:

#replace evey non valid char to underscore
$value =~ s/([^a-zA-Z0-9_\-\.\;])/_/g;