## Aurigma Image Uploader Flash 1.0

# Saving Uploaded Files in PHP

- Collapse All Sections
- See Also
- Language Filter ( all languages )

Flash applications are not allowed to send files created by themselves as standard POST request fields accessible through the $_FILES collection. Image Uploader Flash bypasses this limitation to upload files. However, it causes some problems with retrieving these files on the server side. Image Uploader Flash PHP library which comes with Image Uploader Flash provides a couple of facilities to solve this problem. The present article describes these approaches in detail.

Note

For versions of PHP earlier than 6.0 disable the magic_quotes_gpc directive in your php.ini file.

## Using UploadedFiles Object

This approach implies using the UploadedFiles object which exposes the PackageFilesEnumerator and AllFilesEnumerator properties to access the uploaded data. The first one returns the PackageFilesEnumerator object which provides access to the currently uploaded file. The second property returns the AllFilesEnumerator which iterates through all the files uploaded in the session. Each file is represented with the UploadedFile class which contains all the files and metadata related to a particular user-selected file.

All the data available through the UploadedFile properties can be divided into the following groups:

### Converted Files

This group consists of the ConvertedFiles property which returns an array of converted files. A converted file represents original user-selected file or file created by Image Uploader Flash depending on its configuration (thumbnail, icon, or ZIP compressed file). Converted files are created and sent for each Converter added to the ImageUploaderFlash.Converters array. If you are not familiar with converters read the Configuring Upload Settings topic first.

Each converted file is represented with the FileInfo class. This class has a number of properties which provide access to characteristics of the converted file. To save a converted file you can use the saveAs(string) method which saves this file to the specified path on a server.

### User-provided Data

This group includes the Angle and Description properties which contain the rotation angle and user-provided description respectively.

### Upload Details

This group consists of upload session-related properties. All the files and metadata related to a particular user-selected file are uploaded in a separate package (request). It means that the upload session contains as many packages as there are files in the upload list.

- PackageCount contains total number of packages in the current upload session.
- PackageGuid contains a unique identifier of the current upload session.
- PackageIndex contains index of the current package inside the upload session.

### Original File Information

This group includes properties containing information about the original file.

- SourceCreatedDateTime and SourceCreatedDateTimeLocal represent creation date of the original file.
- SourceLastModifiedDateTime and SourceLastModifiedDateTimeLocal represent last modification date of the original file.
- SourceHeight and SourceWidth represent dimensions of the original file (in pixels) if it is an image.
- SourceXResolution and SourceYResolution represent horizontal and vertical resolutions of the original file (in dpi) if it is an image.
- SourceName represent the original file name.
- SourceSize represent the original file size.

The code sample below demonstrates how to save a source file and its resized copy to the ../Gallery/ and ../Gallery/Thumbnails/ folders respectively and write some file-related information (filenames, image dimensions and user description) to a XML file.

### PHP

    <?php
    require_once "../ImageUploaderFlashPHP/UploadedFiles.php";

    $galleryPath = '../Gallery/';
    $absGalleryPath = realpath($galleryPath) . '/';
    $absThumbnailsPath = realpath($galleryPath . 'Thumbnails/') . '/';

    //Create XML file which will keep information about files (image dimensions, description, etc).
    $descriptions = new DOMDocument('1.0');
    $descriptions->load($absGalleryPath . 'Descriptions.xml');

    //Iterate through uploaded data and save the original file, thumbnail, and description.
    $enumerator = UploadedFiles::getPackageFilesEnumerator();
    while(($file = $enumerator->moveNext()) !== null) {
        $convertedFiles = $file->getConvertedFiles();

        //Save original file.
        //It is the first file in ConvertedFiles collection as we set first converter mode to SourceFile.
        $sourceFile = $convertedFiles[0];
        $sourceFileName = getSafeFileName($absGalleryPath, rawurlencode($file->getSourceName()));
        $sourceFile->saveAs($absGalleryPath . $sourceFileName);

        //Save thumbnail.
        //It is the second file in ConvertedFiles collection as we set second converter mode to Thumbnail.
        $thumbnailFile = $convertedFiles[1];
        $thumbnailFileName = getSafeFileName($absThumbnailsPath, rawurlencode($file->getSourceName()));
        $thumbnailFile->saveAs($absThumbnailsPath . $thumbnailFileName);

        //Save file info.
        $xmlFile = $descriptions->createElement('file');
        $xmlFile->setAttribute('name', $sourceFileName);
        $xmlFile->setAttribute('thumbName', $thumbnailFileName);
        $xmlFile->setAttribute('width', $file->getSourceWidth());
        $xmlFile->setAttribute('height', $file->getSourceHeight());
        $xmlFile->setAttribute('description', $file->getDescription());
        $descriptions->documentElement->appendChild($xmlFile);
    }

    $descriptions->save($absGalleryPath . 'Descriptions.xml');

    // Server upload script should return non-empty response.
    echo "complete";

    function getSafeFileName($path, $fileName) {
        // ...
    }
    ?>

## Autosave

The UploadedFiles class implements the saveFiles(string) method which saves all the uploaded files to the folder specified in its parameter. This feature allows you to handle the upload just writing one line of code:

### PHP

    <?php
    require_once "ImageUploaderFlashPHP/UploadedFiles.php";

    UploadedFiles::saveFiles("../Gallery/");
    ?>

If the specified folder does not exist, this method creates it; however, it does not resolve name conflicts. It means that if the destination folder contains a file with the same name as one of uploaded file, the file will be overwritten.

## Parsing POST Request Manually

Another way lies in parsing the upload request manually via the $_FILES collection. However, firstly, you should link the flashrequestfix.php file with the upload page. This file provides three functions required for saving uploaded files:

- preProcessRequest() adds uploaded files to the $_FILES array. Call this function before parsing the POST requset.
- postProcessRequest() deletes temporary files. Call this function after parsing the POST requset.
- moveFile($source, $destination) saves file to the destination path. Use this function instead of move_uploaded_file($filename, $destination).

Note

If you do not call the postProcessRequest() function, temporary files will be accumulated on your server.

The following code sample demonstrates how to save converted files and original file-related information using this approach.

### PHP

    <?php
    require_once '../ImageUploaderFlashPHP/flashrequestfix.php';

    preProcessRequest();

    $galleryPath = '../Gallery/';
    $absGalleryPath = realpath($galleryPath) . '/';
    $absThumbnailsPath = realpath($galleryPath . 'Thumbnails/') . '/';

    //Create XML file which will keep information about files (image dimensions, description, etc).
    $descriptions = new DOMDocument('1.0');
    $descriptions->load($absGalleryPath . 'Descriptions.xml');

    //Get total number of uploaded files (all files are uploaded in a single package).
    $fileCount = $_POST["PackageFileCount"];

    //Iterate through uploaded data and save the original file, thumbnail, and description.
    for ($i = 0; $i < $fileCount; $i++) {

        $originalFileName = rawurlencode($_REQUEST['SourceName_' . $i]);

        //save source file
        $sourceFileName = getSafeFileName($absGalleryPath, $originalFileName);
        if (isset($_FILES['File0_' . $i])) {
            moveFile($_FILES['File0_' . $i]['tmp_name'], $absGalleryPath . $sourceFileName);
        }

        //save thumbnail file
        $thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName);
        if (isset($_FILES['File1_' . $i])) {
             moveFile($_FILES['File1_' . $i]['tmp_name'], $absThumbnailsPath . $thumbnailFileName);
        }

        //Save file info.
        $xmlFile = $descriptions->createElement('file');
        $xmlFile->setAttribute('name', $sourceFileName);
        $xmlFile->setAttribute('thumbName', $thumbnailFileName);
        $xmlFile->setAttribute('width', $_REQUEST['SourceWidth_' . $i]);
        $xmlFile->setAttribute('height', $_REQUEST['SourceHeight_' . $i]);
        $xmlFile->setAttribute('description', $_REQUEST['Description_' . $i]);
        $descriptions->documentElement->appendChild($xmlFile);

    }

    $descriptions->save($absGalleryPath . 'Descriptions.xml');

    postProcessRequest();

    // Server upload script should return non-empty response.
    echo "complete";

    function getSafeFileName($path, $fileName) {
        // ...
    }
    ?>

## See Also

### Reference

- UploadedFiles Class
- PackageFilesEnumerator Class
- AllFilesEnumerator Class
- UploadedFile Class
- FileInfo Class

### Manual

- Quick Start with PHP Library
- Configuring Upload Settings