Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Batch Uploader Using Flash Action Script 2 or Multiple File Upload Using Flash

3.00/5 (1 vote)
30 Jan 2010CPOL2 min read 22.4K   348  
The Batch Uploader was developed using Action Script 2, and uses PHP as the backend to process the file upload.

Introduction

The Batch Uploader was developed using Action Script 2 and uses PHP as the backend to process the file upload. The current user ID can be passed to the Flash module by defining a FlashVars variable as "userid", which will be passed to the PHP function when albums are requested or when the upload is processed.

Background

Pretty long time back, I developed a batch uploading tool in Flash for a large online photo sharing website. The purpose of the tool was to allow site users to upload photos from their computer to their online account. This Flash tool with the complete source code is available for free here.

Many of the batch process tools available cause errors while processing for a long time, and the browsers try to abort the script by showing a warning alert to the user. I was successful in fixing this issue.

Using the code

Here is an example of passing a variable to Flash from HTML using FlashVars:

HTML
//Object Tag Example:
<PARAM NAME=FlashVars VALUE="userid=10">
//Embed Tag Example:
<EMBED href="display.swf" FlashVars="userid=10"></EMBED>

The application initially requests for albums created by the user, by calling a PHP script which returns a list of albums and their IDs in XML format.

See the code at line 86.

var albumListURL = 
  (_root.albumUrl)?_root.albumUrl:"http://yourwebsite.com/albumList.php";

albumList.php should return the list of albums in XML format, as shown below:

XML
<?xml version="1.0" encoding="utf-8"?>
<albums>
    <album><id>1</id><name>My Little Baby</name></album>
    <album><id>2</id><name>Wedding Photos</name></album>
</albums>

The tool also allows the user to create a new album right from the uploader interface. Open the "albumpop_mc" movie clip on the stage to see the album creation functionalities.

The batch upload tool sends selected files one by one to the server. uploadItem (line number 172) is the function which sends the file to the server PHP script.

flash
if (!itemRef.upload("http://yourwebsite.com/batchUploadProcess.php&album="+
          albumHash+"&user="+_root.userId)) {
    errorList.push("Failed to upload : "+item.name);}

This code sends one file at a time to a PHP script named "batchUploadProcess.php" which receives the file at the server and adds a record in the database. The ID of the album the user has chosen from the list and the user IDd the application is given as a FlashVars variable are sent to the PHP script.

Below is the script I used to process the file sent from Flash:

XML
<?xml version="1.0" encoding="utf-8"?>
<albums>
    <album><id>1</id><name>My Little Baby</name></album>
    <album><id>2</id><name>Wedding Photos</name></album>
</albums>
flash
switch($_FILES['Filedata']['error']) {
    case 1:
    case 2:
    case 3:
    case 6: die; break;
    case 4:
    case 0: break;
    default: die; break;
}
if ($_FILES['Filedata']['error'] == 0) {
    $fileInfo['photo'] = getimagesize($_FILES['Filedata']['tmp_name']);
    if ($fileInfo['photo'][2] != 2) die;
}
move_uploaded_file($_FILES['Filedata']['tmp_name'], 
                   "./photos/originals/YourFileName.jpg");
//Add your other codes here
echo "success";

Click the below link to download the latest source file in CS3 format:

If you are an intermediate Flash programmer, you can easily understand the code flow. Modify and use the code wherever you want. I don't really want to explain the complete code here. But you can contact me if you have any questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)