ActiveX/Java Uploader PHP is a collection of classes which are meant to deploy ActiveX/Java Uploader and handle uploaded data easier. Using this collection, PHP developers can work with ActiveX/Java Uploader in a usual way as with a common PHP object. ActiveX/Java Uploader PHP provides several advantages over a standard way of deployment described in the Quick Start with ActiveX/Java Uploader JavaScript article:
To add ActiveX/Java Uploader to a web page you just need to perform the following steps:
Copy the /ImageUploaderPHP folder to an application. It can be found in ActiveX/Java Uploader installation folder (usually this is C:\Program Files\Aurigma\Upload Suite 8.5.81\ActiveX-Java\PHP\PHPClasses\ImageUploaderPHP).
Link the Uploader.class.php file with the page which will host ActiveX/Java Uploader.
<?php
require_once "ImageUploaderPHP/Uploader.class.php";
?>
Create a new instance of the Uploader class. Then configure ActiveX/Java Uploader in the way described in the following sections. After you have initialized necessary parameters call the render() method which generates ActiveX/Java Uploader embedding code and writes it into the page.
<?php
$uploader = new Uploader("Uploader1");
//configure ActiveX/Java Uploader
$uploader->render();
?>
Here we discuss the use ActiveX/Java Uploader PHP by the example of a simple file catalog. Suppose, the catalog requires to upload files of various types and displays links to download these files. To implement this we should configure an Uploader object and save uploaded files on a server. The sections below describe these steps in detail.
To configure ActiveX/Java Uploader parameters you should set corresponding properties of the Uploader object. For example, the ID property specifies a unique identifier of the control.
Here is the source code of the main page of our catalog (index.php).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ActiveX/Java Uploader PHP Sample</title>
</head>
<body>
<?php
require_once "ImageUploaderPHP/Uploader.class.php";
// create Uploader object and specify its ID and size
$uploader = new Uploader("Uploader1");
$uploader->setWidth("650px");
$uploader->setHeight("480px");
// specify a license key
$uploader->setLicenseKey("XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXXX;YYYYY-YYYYY-YYYYY-YYYYY-YYYYY-YYYYYY");
// configure upload settings
$uploader->getUploadSettings()->setActionUrl("upload.php");
$uploader->getUploadSettings()->setRedirectUrl("catalog.php");
//configure uploaded files
$converters = &$uploader->getConverters();
// one converter for uploading source file
$converter = new Converter();
$converter->setMode("*.*=SourceFile");
$converters[] = $converter;
// render Uploader
$uploader->render();
?>
</body>
</html>
On this page we host ActiveX/Java Uploader and configure it, namely, specify its ID and size, server upload script and the page to which the user will be redirected when the upload successfully completes. To configure ActiveX/Java Uploader to send original files we used the Uploader.Converters property which returns a set of Converter instances. Each instance specifies what will be uploaded (original file as is, thumbnail created from original image file, icon associated with original file, or original file compressed into ZIP archive) for each of the user-selected files. Since we need to upload original files only, we set one converter with the SourceFile mode. Read more about converters in the Configuring Files to be Uploaded in ActiveX/Java Uploader topic.
One more significant property is Uploader.LicenseKey which specifies trial or full license key. If license key is not set ActiveX/Java Uploader will not send files (the exception is usage of uploader on localhost domain). See the Registering ActiveX/Java Uploader topic for details.
The uploaded data can be saved using the UploadHandler.saveFiles(string) method which saves all the received files to the specified folder. This method returns an array of full filenames of saved files. According to the catalog requirements, we store files set to upload by a user in /Catalog folder.
Here is the source code of the upload script (upload.php) used in our catalog.
<?php
require_once "ImageUploaderPHP/UploadHandler.class.php";
$uploadHandler = new UploadHandler();
$uploadHandler->saveFiles("Catalog/");
?>
On the configuring step we specified the UploadSettings.RedirectUrl property to the "catalog.php" value. It means that ActiveX/Java Uploader will redirect users to this page after the upload is successfully completed. It would be convenient to list links to the uploaded files here. To implement this functionality we iterate through all the files stored in the /Catalog folder and display a link to each of them. See the source code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Catalog</title>
</head>
<body>
<?php
$catalogPath = "Catalog/";
//Open catalog directory
if (is_dir($catalogPath)) {
if ($directory = opendir($catalogPath)) {
//Iterate through all files
while (($file = readdir($directory))){
//Process files only
if (!is_dir($file)){
echo "<a href=\"$catalogPath$file\" target=\"_blank\">\n";
echo " $file\n";
echo "</a><br />\n";
}
}
closedir($directory);
}
}
?>
</body>
</html>
Make sure that the /Catalog folder has enough permissions to save files to.
One more example of ActiveX/Java Uploader usage is an image gallery. Suppose, it requires a user to upload original images along with their downsized copies. This task can be divided on the same phases as the previous one.
The configuration of the Uploader object used for our image gallery is almost the same as for the file catalog described above.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ActiveX/Java Uploader PHP Sample</title>
</head>
<body>
<?php
require_once "ImageUploaderPHP/Uploader.class.php";
// create Uploader object and specify its ID and size
$uploader = new Uploader("Uploader1");
$uploader->setWidth("650px");
$uploader->setHeight("480px");
// specify a license key
$uploader->setLicenseKey("XXXXX-XXXXX-XXXXX-XXXXX-XXXXX-XXXXXX;YYYYY-YYYYY-YYYYY-YYYYY-YYYYY-YYYYYY");
// configure upload settings
$uploader->getUploadSettings()->setActionUrl("upload.php");
$uploader->getUploadSettings()->setRedirectUrl("gallery.php");
//configure uploaded files
$converters = &$uploader->getConverters();
// one converter for uploading source file
$converter = new Converter();
$converter->setMode("*.*=SourceFile");
$converters[] = $converter;
// and second - for uploading thumbnail
$converter = new Converter();
$converter->setMode("*.*=Thumbnail");
// set thumbnail size
$converter->setThumbnailWidth(120);
$converter->setThumbnailHeight(120);
$converter->setThumbnailFitMode("Fit");
$converters[] = $converter;
// render Uploader
$uploader->render();
?>
</body>
</html>
The difference is that we set an additional converter to specify a thumbnail which will be created for each image file and will be fitted to 120x120 pixels. Read more about converters in the Configuring Files to be Uploaded in ActiveX/Java Uploader topic.
Here we use the UploadHandler.saveFiles(string) method to save uploaded files to the /Gallery folder, as it is demonstrated in upload.php source code below.
<?php
require_once "ImageUploaderPHP/UploadHandler.class.php";
$uploadHandler = new UploadHandler();
$uploadHandler->saveFiles("Gallery/");
?>
Pay attention to the fact that the names of files stored in the destination folder are not equal to the original filenames. Each name has a suffix corresponding to the converter used to create this file(except for the SourceFile one). In our example we set two converters, that is why there are two files stored for each user-selected file:
See the ConvertedFile.Name property description for a full list of possible suffixes.
On the configuring step we specified the UploadSettings.RedirectUrl property to the "gallery.php" value. It means that ActiveX/Java Uploader will redirect users to this page after the upload is successfully completed. It would be convenient to display uploaded images here. To implement this functionality we iterate through all the files stored in the destination folder and display each thumbnail (file with _Thumbnail1 suffix) as a link to its original image (no suffix). See comments in the source code below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Gallery</title>
</head>
<body>
<?php
$galleryPath = "Gallery/";
//Open catalog directory
if (is_dir($galleryPath)) {
if ($directory = opendir($galleryPath)) {
//Iterate through all files
while (($file = readdir($directory))){
//Process files only
if (!is_dir($file)){
$pos = strpos($file, "_Thumbnail1");
//The file is not a thumbnail, so it is a source file
if ($pos == false){
//Construct name of the corresponded thumbnail
$thumbnailName = $file."_Thumbnail1.jpg";
//Check if this thumbnail exists
if (file_exists("$galleryPath$thumbnailName")){
echo "<a href=\"$galleryPath$file\" target=\"_blank\">\n";
echo " <img src=\"$galleryPath$thumbnailName\" />\n";
echo "</a> \n";
}
}
}
}
closedir($directory);
}
}
?>
</body>
</html>
Make sure that /Gallery folder has enough permissions to save files to.