Re: File Upload In Form

From: Date: Tue, 18 Sep 2001 14:22:22 +0000
Subject: Re: File Upload In Form
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-9403@lists.php.net to get a copy of this message
At 10:39 AM 9/18/2001 +0430, you wrote:
Hi All, I must a file download with used html form, for example with this code: How can upload only GIF or JPG file?
Be sure to include the MAX_FILE_SIZE hidden input field -- it is required. And be sure to put it BEFORE the file input field in the code. As for limiting it to .GIF or .JPG files, you can only check that (without JavaScript) once the file has already been uploaded. You could also verify it with JavaScript, but that is dependent on the client's browser. For more on this, see: http://developer.netscape.com/docs/manuals/communicator/jsref/txt3.htm If you have a basic understanding of field properties and string functions, you can get the file extension from the field when it is changed. Verifying it with PHP is a little tricker, but not much. Here's some code that I use. content_file is the name of the file field from the form. $the_file = $HTTP_POST_FILES['content_file']['tmp_name']; $the_name = $HTTP_POST_FILES['content_file']['name']; $the_type = $HTTP_POST_FILES['content_file']['type']; $the_size = $HTTP_POST_FILES['content_file']['size']; if ($the_file != 'none') {
    $allowed_types = array(
      "text/plain" => ".txt",
      "application/msword" => ".doc",
      "application/pdf" => ".pdf"
    );
    if (in_array($the_type, array_keys($allowed_types))) {
      if (is_uploaded_file($the_file)) {
        move_uploaded_file($the_file, ...destination...);
// proceed with file manipulation & successful form submission
      }
      else {
// possible hack attempt?
      }
    else {
// not an accepted file type
    }
} else { // no file was attached } HTH -Mike

« previous php.windows (#9403) next »