Jump to content

Working with JumpLoader


Recommended Posts

I am using JumpLoader for a Photo Upload service. Each time a user goes to the upload page I want it to create on directory and upload all files to that one directory.

 

So far the way I am doing this is setting a cookie, creating the directory then telling the upload handler to upload to that directory.

 

The folder that is created is a a random number which in turn is the Reference Number which is sent to the stores email where then the files are downloaded and printed in the size they choose.

 

The problem Im having is it is creating a new directory with a new name(referenceNumber) for each file uploaded even though I am using a "while loop" to only generate a referenceNumber and create the folder with the referenceNumber as the name when the cookie "referenceNumber" doesn't exist.

 

The upload Handler is called for every photo that is uploaded.

 

Below is all my code

 

--The page containing the java applet--

<?
while (!isset($_COOKIE['referenceNumber'])) {

	$referenceNum = 0 . 0 . mt_rand(10000, 99999);
	setcookie("referenceNumber", $referenceNum);
	echo 'No Cookie';
	$path = 'upload/' . $_COOKIE['referenceNumber'] ;
	mkdir($path, 0777);

}
?>
<p>
                    <applet id="jumpLoaderApplet" name="jumpLoaderApplet"
code="jmaster.jumploader.app.JumpLoaderApplet.class"
archive="jumploader_z.jar"
width="598"
height="450"
mayscript>
	<param name="uc_uploadUrl" value="uploadFiles.php"/>

	<param name="ac_fireAppletInitialized" value="true"/>
	<param name="ac_fireUploaderFileAdded" value="true"/>
	<param name="ac_fireUploaderFileRemoved" value="true"/>
        <param name="ac_fireUploaderFileMoved" value="true"/>
	<param name="ac_fireUploaderFileStatusChanged" value="true"/>
	<param name="ac_fireUploaderFilesReset" value="true"/>
	<param name="ac_fireUploaderStatusChanged" value="true"/>
	<param name="ac_fireUploaderFilePartitionUploaded" value="true"/>
	<param name="ac_fireUploaderSelectionChanged" value="true"/>
	<param name="ac_fireUploadViewFileOpenDialogFilesSelected" value="true"/>
	<param name="ac_fireMainViewMessageShown" value="true"/>
</applet>
                  </p>
<!-- callback methods -->
<script language="javascript">
/**
 * applet initialized notification
 */
function appletInitialized( applet ) {
	traceEvent( "appletInitialized, " + applet.getAppletInfo() );
}
/**
 * files reset notification
 */
function uploaderFilesReset( uploader ) {
	traceEvent( "uploaderFilesReset, fileCount=" + uploader.getFileCount() );
}
/**
 * file added notification
 */
function uploaderFileAdded( uploader, file ) {
	traceEvent( "uploaderFileAdded, index=" + file.getIndex() );
}
/**
 * file removed notification
 */
function uploaderFileRemoved( uploader, file ) {
	traceEvent( "uploaderFileRemoved, path=" + file.getPath() );
}
/**
 * file moved notification
 */
function uploaderFileMoved( uploader, file, oldIndex ) {
	traceEvent( "uploaderFileMoved, path=" + file.getPath() + ", oldIndex=" + oldIndex );
}
/**
 * file status changed notification
 */
function uploaderFileStatusChanged( uploader, file ) {
	traceEvent( "uploaderFileStatusChanged, index=" + file.getIndex() + ", status=" + file.getStatus() + ", content=" + file.getResponseContent() );
}
/**
 * file partition uploaded notification
 */
function uploaderFilePartitionUploaded( uploader, file ) {
	traceEvent( "uploaderFilePartitionUploaded, index=" + file.getIndex() + ", partition=" + file.getUploadedPartitionCount() + ", response=" + file.getResponseContent() );
}
/**
 * uploader status changed notification
 */
function uploaderStatusChanged( uploader ) {
	traceEvent( "uploaderStatusChanged, status=" + uploader.getStatus() );
}
/**
 * uploader selection changed notification
 */
function uploaderSelectionChanged( uploader ) {
	traceEvent( "uploaderSelectionChanged" );
}
/**
 * upload view open dialog files selected notification
 */
function uploadViewOpenDialogFilesSelected(uploadView, paths ) {
	traceEvent( "uploadViewOpenDialogFilesSelected, paths=" + paths.length );
	for(i = 0; i < paths.length; i++) {
		traceEvent("" + i + ". " + paths[i]);
	}
}
/**
 * main view message shown notification
 */
function mainViewMessageShown(mainView, severity, message) {
	traceEvent( "mainViewMessageShown, severity=" + severity + ", message=" + message );
}
</script>

<!-- debug auxiliary methods -->
<script language="javascript">
/**
 * trace event to events textarea
 */
function traceEvent( message ) {
	document.debugForm.txtEvents.value += message + "\r\n";
}
/**
 * dump status of uploader into html
 */
 function dumpUploaderStatus() {
 	var uploader = document.jumpLoaderApplet.getUploader();
 	//
 	//	dump uploader
 	var uploaderDump = "<strong>Uploader</strong><br>" +
		"Status: " + uploader.getStatus() + "<br>" +
		"Files total: " + uploader.getFileCount() + "<br>" +
		"Ready files: " + uploader.getFileCountByStatus( 0 ) + "<br>" +
		"Uploading files: " + uploader.getFileCountByStatus( 1 ) + "<br>" +
		"Finished files: " + uploader.getFileCountByStatus( 2 ) + "<br>" +
		"Failed files: " + uploader.getFileCountByStatus( 3 ) + "<br>" +
		"Total files length: " + uploader.getFilesLength() + " bytes<br>" +
		"";
 	//
 	//	dump files
 	var filesDump = "<strong>Files</strong><br>";
 	for( i = 0; i < uploader.getFileCount(); i++ ) {
 		var file = uploader.getFile( i );
 		filesDump += "" + ( i + 1 ) + ". path=" + file.getPath +
 			", length=" + file.getLength() +
 			", status=" + file.getStatus() +
 			"<br>";
 	}
	//
	//	set text
	document.getElementById( "uploaderStatus" ).innerHTML = uploaderDump + "<br>" + filesDump;
 }
</script>
<form name="debugForm">
<p>Events:<br>
<textarea name="txtEvents" style="width:100%; font:10px monospace" rows="10" wrap="off" id="txtEvents"></textarea>
</p>

  	<p><input type="button" value="Dump uploader status" onClick="dumpUploaderStatus()">
  	  
  	<input type="button" value="About..." onClick="alert( document.jumpLoaderApplet.getAppletInfo() )">
  <p id="uploaderStatus"></p>
 </p>
</form>

 

--The upload Handler--

<?php
//----------------------------------------------
//    partitioned upload file handler script
//----------------------------------------------
//
//    specify upload directory - storage 
//    for reconstructed uploaded files
$upload_dir = "upload/" . $_COOKIE['referenceNumber'] . '/';

//
//    specify stage directory - temporary storage 
//    for uploaded partitions
$stage_dir = $_SERVER[ 'DOCUMENT_ROOT' ] . "/uploaded/stage/";

//
//    retrieve request parameters
$file_param_name = 'file';
$file_name = $_FILES[ $file_param_name ][ 'name' ];
$file_id = $_POST[ 'fileId' ];
$partition_index = $_POST[ 'partitionIndex' ];
$partition_count = $_POST[ 'partitionCount' ];
$file_length = $_POST[ 'fileLength' ];

//
//    the $client_id is an essential variable, 
//    this is used to generate uploaded partitions file prefix, 
//    because we can not rely on 'fileId' uniqueness in a 
//    concurrent environment - 2 different clients (applets) 
//    may submit duplicate fileId. thus, this is responsibility 
//    of a server to distribute unique clientId values
//    (or other variable, for example this could be session id) 
//    for instantiated applets.
$client_id = $_GET[ 'clientId' ];

//
//    move uploaded partition to the staging folder 
//    using following name pattern:
//    ${clientId}.${fileId}.${partitionIndex}
$source_file_path = $_FILES[ $file_param_name ][ 'tmp_name' ];
$target_file_path = $stage_dir . $client_id . "." . $file_id . 
    "." . $partition_index;
if( !move_uploaded_file( $source_file_path, $target_file_path ) ) {
    echo "Error:Can't move uploaded file";
    return;
}

//
//    check if we have collected all partitions properly
$all_in_place = true;
$partitions_length = 0;
for( $i = 0; $all_in_place && $i < $partition_count; $i++ ) {
    $partition_file = $stage_dir . $client_id . "." . $file_id . "." . $i;
    if( file_exists( $partition_file ) ) {
        $partitions_length += filesize( $partition_file );
    } else {
        $all_in_place = false;
    }
}

//
//    issue error if last partition uploaded, but partitions validation failed
if( $partition_index == $partition_count - 1 &&
        ( !$all_in_place || $partitions_length != intval( $file_length ) ) ) {
    echo "Error:Upload validation error";
    return;
}

//
//    reconstruct original file if all ok
if( $all_in_place ) {
    $file = $upload_dir . $client_id . "." . $file_id;
    $file_handle = fopen( $file, 'w' );
    for( $i = 0; $all_in_place && $i < $partition_count; $i++ ) {
        //
        //    read partition file
        $partition_file = $stage_dir . $client_id . "." . $file_id . "." . $i;
        $partition_file_handle = fopen( $partition_file, "rb" );
        $contents = fread( $partition_file_handle, filesize( $partition_file ) );
        fclose( $partition_file_handle );
        //
        //    write to reconstruct file
        fwrite( $file_handle, $contents );
        //
        //    remove partition file
        unlink( $partition_file );
    }
    fclose( $file_handle );
    //
    // rename to original file
    // NB! This may overwrite existing file
    $filename = $upload_dir . $file_name;
    rename($file,$filename);
}
//
//	below is trace of request variables
?>
<html>
<body>
<h1>GET content</h1>
<pre><?print_r( $_GET );?></pre>
<h1>POST content</h1>
<pre><?print_r( $_POST );?></pre>
<h1>FILES content</h1>
<pre><?print_r( $_FILES );?></pre>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.