Source for file upload.php
Documentation is available at upload.php
* Example for HTML_QuickForm_Controller
* Handling file uploads and dynamic form generation
* @version SVN: $Id: upload.php 289084 2009-10-02 06:53:09Z avb $
* @author Bertrand Mansion <bmansion@mamasam.com>
// For this example to work, you'll need to create an 'uploads' directory
// in the directory where this script is located and give write permissions
// on it to your webserver.
require_once 'HTML/QuickForm/Controller.php';
require_once 'HTML/QuickForm/Action/Display.php';
// Will propose 2 layouts.
// The selection will change the 2nd page.
$this->_formBuilt = true;
$image = '<div class="image">O</div>';
$text = '<div class="text">In the next page, you will write your text in this box. The other box besides will contain the picture that will have to be uploaded.</div>';
$radios[0 ] = & $this->createElement ('radio', null , $image. $text, ' ', 'A');
$radios[1 ] = & $this->createElement ('radio', null , $text. $image, ' ', 'B');
$this->addGroup ($radios, 'layout', 'Choose a layout');
$buttons[0 ] = & $this->createElement ('submit', $this->getButtonName('next'), 'Next step >>');
$this->addGroup ($buttons, 'buttons', '', ' ', false );
// Layout will reflect choices made in the first page.
$this->_formBuilt = true;
$this->registerRule ('isImage', 'callback', '_ruleIsImage', get_class($this));
$text = & $this->createElement ('textarea', 'content', 'Type text here...', array ('cols'=>30 , 'rows'=>4 ));
$upped = & $this->createElement ('file', 'file', 'Upload your image here...');
if ($this->controller->exportValue ('page1', 'layout') == 'A') {
$this->addGroup (array ($upped, $text), 'contents', 'Enter the contents', null , false );
$this->addGroup (array ($text, $upped), 'contents', 'Enter the contents', null , false );
$prevnext[] = & $this->createElement ('submit', $this->getButtonName('back'), '<< Previous step');
$prevnext[] = & $this->createElement ('submit', $this->getButtonName('upload'), 'Preview >>');
$this->addGroup ($prevnext, 'buttons', '', ' ', false );
$rules['file'][0 ] = array ('Must be *.jpg, *.gif or *.png', 'filename', '/\.(jpe?g|gif|png)$/i');
$rules['file'][1 ] = array ('The file must be an image', 'isImage');
$this->addGroupRule ('contents', $rules);
function _ruleIsImage ($data)
((isset ($data['error']) && 0 == $data['error']) ||
(!empty ($data['tmp_name']) && 'none' != $data['tmp_name'])) &&
return is_array($info) && (1 == $info[2 ] || 2 == $info[2 ] || 3 == $info[2 ]);
// Will show the preview for the text and the image
$this->_formBuilt = true;
if (!empty ($data['_upload'])) {
$image = '<img src="uploads/'. $data['_upload']. '" alt="uploaded image" />';
$theTxt = & HTML_QuickForm ::createElement ('static', 'thetext', 'Your text...', $text);
$theImg = & HTML_QuickForm ::createElement ('static', 'thefile', 'Your file...', $image);
if ($this->controller->exportValue ('page1', 'layout') == 'A') {
$this->addGroup (array ($theImg, $theTxt), 'contents', 'Your contents', null , false );
$this->addGroup (array ($theTxt, $theImg), 'contents', 'Your contents', null , false );
$prevnext[] = & $this->createElement ('submit', $this->getButtonName('back'), '<< Previous step');
$prevnext[] = & $this->createElement ('submit', $this->getButtonName('next'), 'Finish');
$this->addGroup ($prevnext, 'buttons', '', ' ', false );
// Special action for dealing with uploads
function perform(&$page, $actionName)
$page->isFormBuilt () or $page->buildForm ();
$pageName = $page->getAttribute ('id');
$data = & $page->controller ->container ();
$data['values'][$pageName] = $page->exportValues ();
if (PEAR ::isError ($valid = $page->validate ())) {
$data['valid'][$pageName] = $valid;
if (!$data['valid'][$pageName]) {
return $page->handle ('display');
// get the element containing the upload
$group = & $page->getElement ('contents');
$elements = & $group->getElements ();
if ('file' == $elements[$key]->getType ()) {
// move the file and store the data
if ($elements[$key]->isUploadedFile ()) {
$elements[$key]->moveUploadedFile ('./uploads/');
$value = $elements[$key]->getValue ();
if (!empty ($data['_upload'])) {
@unlink('./uploads/' . $data['_upload']);
$data['_upload'] = basename($value['name']);
$next = & $page->controller ->getPage ($page->controller ->getNextName ($pageName));
// Class for form rendering
function _renderForm (&$page)
require_once 'HTML/Template/Sigma.php';
require_once 'HTML/QuickForm/Renderer/ITDynamic.php';
$tpl = & new HTML_Template_Sigma ('./templates');
$tpl->loadTemplateFile ('upload.html');
$renderer = & new HTML_QuickForm_Renderer_ITDynamic ($tpl);
$renderer->setElementBlock (array (
'buttons' => 'qf_buttons',
'contents' => 'qf_group_table'
$page->accept ($renderer);
// Class for form processing
function perform(&$page, $actionName)
$values = $page->controller ->exportValues ();
$data = & $page->controller ->container ();
$wizard->setDefaults (array ('layout' => 'A'));
Documentation generated on Mon, 11 Mar 2019 15:34:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|