input type=file, file upload control

inputFile

A simple file selector, like this: .

This control aborts with an error message if file upload is not allowed by php.ini (file_uploads=0).

When you need modify forms

Once you're in a need to create a modify form including this element, you can use a few extra settings to create common controls like

  • a "delete file" button that opens a given URL
  • a text link or a clickable image preview (or icon) of the file already uploaded.

    To display such a link you need to use the href option. Instead of long explanation of other settings some pseudo code follows (not used in this manner but it's much easier to follow):

    if ( strlen( $settings['thubmnail'] ) )
      $linktext = $settings['thumbnail'];
    elseif ( strlen( $settings['text'] ) )
      $linktext = $settings['text'];
    else 
      $linktext = basename( $settings['href'] );
    echo '<a href="' . $settings['href'] . '">' . $linktext . '</a>';
    The actual code is a little different as you can alter the tags themselves using linklayout and thumbnaillayout, etc.

When you need to get the binary value

You may use the binaryvalue setting (set to 1) to get the binary file contents returned by any of the clonefish methods (getElementValue(), getValues() ) after a successful validation. Further notes:

  • The contents of the uploaded file is loaded from the $_FILES['inputname']['tmp_name'] file.
  • The null PHP value is returned by the value getter methods after submitting an invalid file (not false, since that's a one-byte long value which is misleading).
  • If you've set the value attribute of the file input previously, it'll be overwritten if an invalid file was submitted.

<?php

$config 
= Array(

  
'portrait' => Array(
    
'type'           => 'inputFile',
    
'displayname'    => 'Choose a picture',

    
// optional settings
    
'href' =>      // URL of the file itself
      
'http://www.example.com/uploaded/images/12.gif',
    
'text' =>      // link text - used if the thumbnail is unused
      
'click here to download the current file',
    
'thumbnail' => // thumbnail used as clickable image when set
      
'http://www.example.com/uploaded/images/thumbs/12.gif',
    
'delete' =>    // displays a button with onclick handler to open URL
      
'somescript.php?target=delete&filename=something.gif',

    
// layout settings
    
'linklayout' => 
      
'<a target="_blank" href="%href%">%thumbortext%</a><br />',
    
'thumbnaillayout' => 
      
'<img src="%thumbnail%" border="0" alt="%text%" />',
    
'deletelayout' => 
      
'<br /><input type="button" value="' STR_IMAGE_DELETE '" 
      onclick="location.href='
%delete%';" />';

    
// misc
    
'binaryvalue' => '1 | 0'// default: 0

    
'html'           => 'class="inputfieldstyle"',
    
'help'           => 'validation failed for this element',
    
'rowlayout'      => '...%element% %prefix% %postfix% etc...',
    
'prefix'         => 'string to display before element',
    
'postfix'        => 'string to display after element',
    
'readonly'       => 0,
    
'display'        => 0,
    
'htmlid'         => 'name1',
);
?>


Live examples:

Validators to use together with this element:

form validation for developers!

clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain forms
learn more
Bookmark and Share