UploadField |
UploadField (
string |
$title |
string |
$name |
array |
$config = array(), |
mixed |
$validator = null, |
string |
$extra = null, |
boolean |
$alertOverwrite = true |
) |
Arguments:
Name |
Description |
$title |
Title of the field |
$name |
Name of the field |
$config |
The configuration options for the upload field. The default options are defined in the config.inc.php file with the var FH_DEFAULT_UPLOAD_CONFIG.
- path - Directory name where the file must be uploaded (default: "./uploads").
- type - The types which are permitted seperated by a space(like: "jpg gif png")
- mime - The allowed mime types. For more information about the mime types see section below.
- size - The maximum size of the image (in bytes). Default this is the maximum permitted size (from the php.ini).
- name -The name of the file (without extension!). When no name is set, the original name is kept.
- width - The maximum allowed width of the uploaded image. This option will only be used on images!
- height - The maximum allowed height of the uploaded image. This option will only be used on images!
- required - if the field is required or not (default: false).
- exists - What to do when the file exists:
"overwrite" -> overwrite the current file
"rename" -> rename the new file
"alert" -> show an alert message and do not upload the file
|
$validator |
This can either be the name of your own validation function or the constant name of a predefined validator function. For more information about validators see Validators.
It is also possible to use a method as a validation function. In this case you should pass an array where the first item is the object and the second item the name of the method. |
$extra |
This can be extra information which will be included in the fields html tag. You can add for example some CSS or javascript. |
$alertOverwrite |
When using the database option and there was already a file uploaded (saved in the field), should a message being showed that uploading a new file will overwrite the current value. |
Description:
This function generates a UploadField.
Only the filename is saved, not the path! (When using the database option.)
The extension of the file is automaticly checked by javascript (if FH_UPLOAD_JS_CHECK is set to true (default)), so that the visitor don't have to wait until his file is uploaded!
Mime types
By default, FormHandler checks the mime type of the uploaded file. For this, the file FH3/includes/mimeTypes.php is used. By doing this, it is not possible to upload a php file by renaming it to .jpg (for example).
However, it can be that the build-in formhandler mime type check is too strict. In this case, you can specify which mime types are allowed.
You can specify the mime types which are (also) allowed in 2 ways:
- match all
- per file extension
If you don't want to set the mime types per extension, all the extensions will be checked for this mime type. You can do this as follows:
<?php
// setting mime types for all extensions
$config = array(
"type" => "jpg jpeg jpe"
"mime" => "image/jpeg image/jpg"
);
// or like this
$config = array(
"type" => "jpg jpeg jpe"
"mime" => array("image/jpeg", "image/jpg")
);
?>
|
If you want to specify mime types per extension, it can be done like this:
<?php
// setting mime types per file extension
$config = array(
"type" => "jpg gif png",
"mime" => array(
"jpg" => "image/jpeg image/jpg",
"gif" => "image/gif",
"png" => "image/png"
)
);
// or like this
$config = array(
"type" => "jpg gif png",
"mime" => array(
"jpg" => array("image/jpeg", "image/jpg"),
"gif" => array("image/gif"),
"png" => array("image/png")
)
);
?>
|
Example
Example: config array
<?php
$config = array (
"path" => "./uploads",
"type" => "jpg jpeg png gif",
"mime" => array(
"jpg" => "image/jpeg image/jpg",
"png" => "image/png",
"gif" => "image/gif"
),
"size" => 1000,
"name" => "test",
"width" => 800,
"height" => 600,
"required" => false,
"exists" => "rename"
);
?>
|
Example:
<?php
// include the class
include('FH3/class.FormHandler.php');
// make a new $form object
$form = new FormHandler();
// The upload configuration
// NOTE: You dont have to set every value!
// Like below, we have not set the "size", so the default configuration
// value is used (max size which is possible).
$cfg = array(
"path" => $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF']).'/uploads/images',
"type" => "jpg jpeg",
"name" => "", // <-- keep the original name
"required" => true,
"exists" => "rename"
);
// upload field
$form -> uploadField("Image", "image", $cfg);
// buttons
$form -> submitButton("Save");
// set the 'commit after form' function
$form -> onCorrect("doRun");
// flush the form
$form -> flush();
// the 'commit after form' function
function doRun($data)
{
echo
"The image is saved: \n".
"<img src='uploads/images/". $data["image"] ."'>\n";
}
?>
|
Preview image
See also: GetFileInfo | IsUploaded | ResizeImage | MergeImage
Latest change: 05 March 10 / 08:35
Comments
|
|