tags:

views:

131

answers:

1

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?

+2  A: 

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).

ysth