Michael Shnitzer
Michael Shnitzer

Reputation: 2505

How can I convert a PHP upload-file script to Perl?

In PHP I have the following code that gets a file submitted through CGI:

move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)

The file is being sent as

Content-Disposition: form-data; name="userfile"; filename="filename"
Content-Type: application/octet-stream

The file being upload is of type PNG.

How do I access the data that $_FILES is getting through perl?

I can use CGI.pm with

$query->upload("filename");

But how do I get the data that is in the "Content-Disposition" part of the header?

Upvotes: 0

Views: 447

Answers (1)

ysth
ysth

Reputation: 98498

To quote the doc:

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. Future browsers may send other information as well (such as modification date and size). To retrieve this information, call uploadInfo(). It returns a reference to a hash containing all the document headers.

   $filename = param('uploaded_file');
   $type = uploadInfo($filename)->{'Content-Type'};
   unless ($type eq 'text/html') {
      die "HTML FILES ONLY!";
   }

Though the param() ought to return the filename too (also usable as a filehandle, but that's deprecated and upload() should be used for that instead).

Upvotes: 2

Related Questions