## Aurigma Image Uploader 7.0.11

# Uploading Folders in PHP

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

Uploading folders is extremely useful when users are allowed to upload large amounts of files. This topic describes how to set up uploading folders on client side and restore folders structure on a server.

Note

Image Uploader Express does not display and upload folders. See the Comparison of Image Uploader Editions topic for details.

## Uploading Folders on Client Side

Configuring folders uploading in Image Uploader is straight-forward and simple to understand. You use the only one property - Uploader.FolderProcessingMode to say to the control how to deal with folders. Let us describe all FolderProcessingMode possible values:

- Skip does not show subfolders in the folder pane, so it is impossible to select a whole folder if it is not a current folder. Only files are uploaded.
- Show makes subfolders visible in the folder pane, so a user can select whole folders. However, restoring folders structure on server side is impossible, because no file path information is uploaded. Only files are uploaded, even if a user selects a folder.
- Upload shows subfolders in the folder pane, so a user can select whole folders. Moreover, it is possible to restore folders structure on a server, because Image Uploader sends each file along with its path relative to the selected folder. Folders and files are uploaded.

Note

When a user selects a folder to upload all its subfolders become selected recursively, in Show and Upload modes.

To allow uploading folder set the FolderProcessingMode property to Upload, like it is shown in the following configuration:

### PHP

    <?php
        require_once "ImageUploaderPHP/Uploader.class.php";
        $uploader = new Uploader("Uploader1");
        $uploader->setFolderProcessingMode("Upload");
        $uploader->render();
    ?>

In this case, if a user selects a whole folder, all the files located in this folder will be recursively added to the upload pane; so a user can clearly see all the files selected for upload at once. When a user clicks Upload, the structure of this folder will be sent to a server via relative paths to each of the selected files. For example, if a user sends a MyDocuments folder, which contains a MyPhotos subfolder, all files inside this subfolder will be sent to a server along with MyDocuments\MyPhotos\filename relative paths and all files contained directly in the MyDocuments folder will have MyDocuments\filename relative paths.

Note

Image Uploader sends only relative paths along with uploaded files, never absolute, because of the security reason.

## Restoring Folders Structure on Server Side

There are three ways to restore folders structure on server side corresponding to approaches described in the Saving Uploaded Files in PHP topic. Let us examine each of these ways in detail, beginning with the simplest one.

### Restoring Folders Structure Automatically

In this case all the uploaded files and folders are saved on a server automatically. All you need is just to create an UploadHandler instance, in the script specified by the UploadSettings.ActionUrl property, and call its UploadHandler.saveFiles(string) method providing a path to the folder where uploaded files should be saved as a parameter. If you are unfamiliar with the UploadHandler class, please, see the server-side components section.

Here is the example which allows uploading folders and saves files and folders automatically to the Gallery folder of your site.

### PHP

    <?php
        require_once "ImageUploaderPHP/UploadHandler.class.php";

        $handler = new UploadHandler();
        $handler->saveFiles("Gallery/");
    ?>

### Restoring Folders Structure Using Server-side Component

Here you recreate uploaded folders manually using the UploadHandler server-side component. If you are unfamiliar with it, please, read the server-side components section first. The UploadHandler class exposes the FileUploadedCallback and AllFilesUploadedCallback properties allowing you to set callback functions giving access all the uploaded file details via the UploadedFile properties. In particular, a relative path is available through the UploadedFile.RelativePath property. For more information about these callbacks see the extended processing of uploaded data section.

The following example uses the FileUploadedCallback property to create a folder for the currently uploaded file (if it was uploaded within a folder, of course) and save this file in it.

### PHP

    <?php
        require_once "ImageUploaderPHP/UploadHandler.class.php";

        function saveUploadedFile($uploadedFile) {
            $absGalleryPath = realpath("Catalog/") . DIRECTORY_SEPARATOR;
            $convertedFiles = $uploadedFile->getConvertedFiles();
            $sourceFile = $convertedFiles[0];
            $destFolder = $absGalleryPath . $uploadedFile->getRelativePath();
            $relativePath = $uploadedFile->getRelativePath();
            if (!file_exists($destFolder))
            {
                mkdir($destFolder, 0777, true);
            }
            $sourceFile->moveTo($destFolder . DIRECTORY_SEPARATOR . rawurlencode($uploadedFile->getSourceName()));
        }
        $handler = new UploadHandler();
        $handler->setFileUploadedCallback("saveUploadedFile");
        $handler->processRequest();
    ?>

### Restoring Folders Structure Using POST Fields Data

This method requires only the standard PHP $_POST "superglobal" to get a relative path to the uploaded file. The path is stored in the SourceName field of received POST request, so you can use it to restore folder structure manually. We also recommend you to use the PostFields class to get names of POST fields. For more information about saving files this way, see the parsing POST requests using PHP predefined variables section.

The example below parses the received request via the standard PHP $_POST and $_FILES arrays, creates uploaded folders, and saves files to them.

### PHP

    <?php
        require_once "ImageUploaderPHP/PostFields.class.php";

        $absGalleryPath = realpath("Gallery/");
        $fileCount = $_POST[PostFields::packageFileCount];
        for ($i = 0; $i < $fileCount; $i++) {
            if (isset($_FILES[sprintf(PostFields::file, 0, $i)])) {
                $destFolder = $absGalleryPath . DIRECTORY_SEPARATOR . $_POST[sprintf(PostFields::sourceName, $i)];
                $destFolder = substr($destFolder, 0, strrpos($destFolder, "\\"));
                if (!file_exists($destFolder))
                {
                    mkdir($destFolder, 0777, true);
                }
                move_uploaded_file($_FILES[sprintf(PostFields::file, 0, $i)]["tmp_name"], $absGalleryPath .
                    DIRECTORY_SEPARATOR . $_POST[sprintf(PostFields::sourceName, $i)]);
            }
        }
    ?>

## See Also

### Reference

- Uploader Class
- UploadHandler Class
- UploadedFile Class
- PostFields Class

### Manual

- Saving Uploaded Files in PHP