Quite often it is necessary to send additional information along with files. Image Uploader Flash solves this task and supports uploading the following kinds of additional information:
This topic has two paragraphs, namely, uploading common data and uploading image-specific data. Each of them describes approaches on how to send and receive relevant data, possibilities provided by these approaches, and code samples.
If you are unfamiliar with the Saving Uploaded Files in PHP topic, please, read it first.
This section describes several approaches to uploading additional data. They are quite different but have two features in common, namely, independence of file types and upload of common data.
Using HTML forms is the most straight-forward way to upload data in web applications, thus, Image Uploader Flash supports it. All you need is just to add the desired form to the same page where the ImageUploaderFlash class instance is hosted and set the Metadata.AdditionalFormName property to the name of created form. Then, when a user clicks Upload, the form data will be submitted along with user-selected files. On the server side, the fields of this form are accessible through the Package.PackageFields collection by their names.
An additional form can contain HTML input elements of different types, for example, you may use:
text
for author's namecheckbox
to determine whether to send a notification about the upload completion or notselect
to choose keywords related to the uploadThe following input types are not allowed: file
, image
, button
, and
reset
. Their values will not be sent at all.
The following configuration makes Image Uploader Flash sending data from the addForm
form
along with uploaded files.
<form id="addForm" name="addForm"> <input ID="keywords" name="keywords" type="text" /> <input ID="addressee" name="addressee" type="text" /> </form> <?php require_once "ImageUploaderFlashPHP/ImageUploaderFlash.class.php"; $uploader = new ImageUploaderFlash("Uploader1"); $uploader->getUploadSettings()->setActionUrl("upload.php"); $uploader->getMetadata()->setAdditionalFormName("addForm"); $uploader->render(); ?>
Fields of the additional form are sent in each package, though in the following example we use package containing the first file, which number is 0
,
because there is always at least one file in the upload session.
<?php require_once "ImageUploaderFlashPHP/UploadHandler.class.php"; function saveAllUploadedFiles($uploadedFiles) { $packageFields = $uploadedFiles[0]->getPackage()->getPackageFields(); $keywords = $packageFields["keywords"]; $addressee = $packageFields["addressee"]; } $handler = new UploadHandler(); $handler->setAllFilesUploadedCallback("saveAllUploadedFiles"); $handler->processRequest(); ?>
Additional fields can be attached to the POST request using the metadata.addCustomField(String, String, Boolean) method. It accepts the following parameters:
You may add custom fields to the upload request only at runtime using the Image Uploader Flash JavaScript API. Custom fields can use any format you like but should not conflict with the standard field names. See the POST Field Reference topic for a full list of standard Image Uploader Flash fields.
On the server side, additional fields are accessible through the Package.PackageFields collection by their names.
The following example sends and receives an additional field named authorField
. Here the metadata.addCustomField(String, String, Boolean) method is called in the BeforeUpload event handler,
which fires when the upload is about to be started. For more information about events, please, see the using events section.
<script type="text/javascript" language="javascript"> function onBeforeUpload() { $au.imageUploaderFlash('Uploader1').metadata().addCustomField('authorField', 'Author Name'); } </script> <?php require_once "ImageUploaderFlashPHP/ImageUploaderFlash.class.php"; $uploader = new ImageUploaderFlash("Uploader1"); $uploader->getUploadSettings()->setActionUrl("upload.php"); $uploader->getClientEvents()->setBeforeUpload("onBeforeUpload"); $uploader->render(); ?>
Additional fields are sent in each package, though in the following example we use package containing the first file, which number is 0
,
because there is always at least one file in the upload session.
<?php require_once "ImageUploaderFlashPHP/UploadHandler.class.php"; function saveAllUploadedFiles($uploadedFiles) { $packageFields = $uploadedFiles[0]->getPackage()->getPackageFields(); $author = $packageFields["authorField"]; } $handler = new UploadHandler(); $handler->setAllFilesUploadedCallback("saveAllUploadedFiles"); $handler->processRequest(); ?>
Each file uploaded via Image Uploader Flash may have a description. There is no need to turn this possibility on, because it is enabled by default. So, let us examine how to get descriptions of uploaded files on a server using the callback function specified by the UploadHandler.FileUploadedCallback property:
<?php require_once "ImageUploaderFlashPHP/UploadHandler.class.php"; function saveUploadedFile($uploadedFile) { $description = $uploadedFile->getDescription(); } $handler = new UploadHandler(); $handler->setFileUploadedCallback("saveUploadedFile"); $handler->processRequest(); ?>
Image Uploader Flash has some imaging capabilities; let us describe two of these features, which allow uploading additional data:
A rotation angle can be got through the Angle property of the UploadedFile class. Image Uploader Flash measures rotation angle in degrees clockwise.
As soon as rotation of images is allowed by default, there is no need to configure Image Uploader Flash to send it, so the following example shows only how to receive such data on a server.
<?php require_once "ImageUploaderFlashPHP/UploadHandler.class.php"; function saveUploadedFile($uploadedFile) { $angle = $uploadedFile->getAngle(); } $handler = new UploadHandler(); $handler->setFileUploadedCallback("saveUploadedFile"); $handler->processRequest(); ?>
To upload EXIF values, you should specify the desired fields using the Metadata.Exif and Metadata.Iptc properties. Both these properties represent semicolon separated lists of field names which should be extracted and uploaded along with images. Metadata.ValueSeparator sets a string separator for fields which can contain multiple string values, like IptcKeyword.
The following example sends two EXIF and two IPTC fields for each user-selected image:
<?php require_once "ImageUploaderFlashPHP/ImageUploaderFlash.class.php"; $uploader = new ImageUploaderFlash("Uploader1"); $uploader->getUploadSettings()->setActionUrl("upload.php"); $converters = &$uploader->getConverters(); $converter = new Converter(); $converter->setMode("*.*=SourceFile"); $converters[] = $converter; $uploader->getMetadata()->setIptc("IptcCopyrightNotice;IptcKeyword"); $uploader->getMetadata()->setExif("ExifColorSpace;ExifDateTime"); $uploader->getMetadata()->setValueSeparator(";"); $uploader->render(); ?>
The following example gets the EXIF and IPTC fields sent accordingly to the previous configuration and writes these values to the
Descriptions.xml
file. Here uploaded fields are contained in the Package.PackageFields
collection and can be retrieved by keys named in the following way: FieldName_xx
, where FieldName
is a name of an EXIF or IPTC field and xx
is the index of an uploaded file in this package.
<?php require_once "ImageUploaderFlashPHP/UploadHandler.class.php"; function saveUploadedFile($uploadedFile) { $absGalleryPath = realpath("Gallery/"); $packageFields = $uploadedFile->getPackage()->getPackageFields(); $descriptions = new DOMDocument('1.0'); if (file_exists($absGalleryPath . "/Descriptions.xml")) $descriptions->load($absGalleryPath . "/Descriptions.xml"); else $descriptions->appendChild($descriptions->createElement("files")); $xmlFile = $descriptions->createElement("file"); $xmlFile->setAttribute("name", $uploadedFile->getSourceName()); $xmlFile->setAttribute("ExifColorSpace", $packageFields["ExifColorSpace_" . $uploadedFile->getIndex()]); $xmlFile->setAttribute("ExifDateTime", $packageFields["ExifDateTime_" . $uploadedFile->getIndex()]); $descriptions->documentElement->appendChild($xmlFile); $descriptions->save($absGalleryPath . "/Descriptions.xml"); } $handler = new UploadHandler(); $handler->setFileUploadedCallback("saveUploadedFile"); $handler->processRequest(); ?>
All date/time values have the following format: YYYY:MM:DD hh:mm:ss
. For example, May 11, 2006,
2:40PM would be represented as: 2006:05:11 14:40:00
.