CREATING A FILE UPLOAD FIELD

    print $query->filefield(-name=>'uploaded_file',
			    -default=>'starting value',
			    -size=>50,
			    -maxlength=>80);
	-or-

    print $query->filefield('uploaded_file','starting value',50,80);

filefield will return a file upload field for Netscape 2.0 browsers. In order to take full advantage of this you must use the new multipart encoding scheme for the form. You can do this either by calling startform() with an encoding type of $CGI::MULTIPART, or by calling the new method start_multipart_form() instead of vanilla startform().

Parameters
  • The first parameter is the required name for the field (-name).

  • The optional second parameter is the starting value for the field contents to be used as the default file name (-default).

    The beta2 version of Netscape 2.0 currently doesn't pay any attention to this field, and so the starting value will always be blank. Worse, the field loses its ``sticky'' behavior and forgets its previous contents. The starting value field is called for in the HTML specification, however, and possibly later versions of Netscape will honor it.

  • The optional third parameter is the size of the field in characters (-size).

  • The optional fourth parameter is the maximum number of characters the field will accept (-maxlength).

  • When the form is processed, you can retrieve the entered filename by calling param.

           $filename = $query->param('uploaded_file');
    

    In Netscape Gold, the filename that gets returned is the full local filename on the remote user's machine. If the remote user is on a Unix machine, the filename will follow Unix conventions:

    	/path/to/the/file
    

    On an MS-DOS/Windows and OS/2 machines, the filename will follow DOS conventions:

    	C:\PATH\TO\THE\FILE.MSW
    

    On a Macintosh machine, the filename will follow Mac conventions:

    	HD 40:Desktop Folder:Sort Through:Reminders
    

    The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls:

    	# Read a text file and print it out
    	while (<$filename>) {
    	   print;
    	}
    

    	# Copy a binary file to somewhere safe
    	open (OUTFILE,">>/usr/local/web/users/feedback");
    	while ($bytesread=read($filename,$buffer,1024)) {
    	   print OUTFILE $buffer;
    	}
    

    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 an associative array containing all the document headers.

           $filename = $query->param('uploaded_file');
           $type = $query->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.

    JAVASCRIPTING: The -onChange, -onFocus, -onBlur and -onSelect parameters are recognized. See textfield for details.