Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter + plupload for file uploading
#1

[eluser]murphybr[/eluser]
I am trying to understand and use a basic widget from the examples Custom Upload
(using CI 2.x)
I have pasted my View:

Code:
Javascript paths for the following(plupload.full.js, jquery-1.6.1.min.js,jquery.plupload.queu.js)


<div id="container">
    <div id="filelist"></div>
    <br />
    <a id="pickfiles" href="#">[Select files]</a>
    <a id="uploadfiles" href="#">[Upload files]</a>
</div>

<s.c.r.i.p.t t.y.p.e="t.e.x.t./.j.a.v.a.s.c.r.i.p.t">
// Convert divs to queue widgets when the DOM is ready
$(function() {
    // Setup html5 version
    $("#html5_uploader").pluploadQueue({
        // General settings
        runtimes : 'html5',
        url : 'FilesTransfer.php/do_upload',
        max_file_size : '2048mb',
        chunk_size : '100mb',
        unique_names : true,
    });

    // Setup html4 version
    $("#html4_uploader").pluploadQueue({
        // General settings
        runtimes : 'html4',
        url : 'FilesTransfer.php/do_upload'
    });
    
    var uploader = new plupload.Uploader({
        runtimes : 'gears,html5,flash,silverlight,browserplus',
        browse_button : 'pickfiles',
        container : 'container',
        max_file_size : '10mb',
        url : 'FileTransfer.php/do_upload',
    });

    uploader.bind('Init', function(up, params) {
        $('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
    });

    $('#uploadfiles').click(function(e) {
        uploader.start();
        e.preventDefault();
    });

    uploader.init();

    uploader.bind('FilesAdded', function(up, files) {
        $.each(files, function(i, file) {
            $('#filelist').append(
                '<div id="' + file.id + '">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' +
            '</div>');
        });
    
        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('UploadProgress', function(up, file) {
        $('#' + file.id + " b").html(file.percent + "%");
   });

    uploader.bind('Error', function(up, err) {
        $('#filelist').append("<div>Error: " + err.code +
            ", Message: " + err.message +
            (err.file ? ", File: " + err.file.name : "") +
            "</div>"
        );

        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('FileUploaded', function(up, file) {
        $('#' + file.id + " b").html("100%");
    });
});
</.s.c.r.i.p.t>
&lt;/body&gt;

my Controller is as follows:
Code:
function do_upload()
    {
        ini_set('upload_max_filesize', '2000M');  
        ini_set('post_max_size', '2000M');  
        ini_set('max_input_time', 900);  
        ini_set('max_execution_time', 900);  
  
        $user = $_SESSION['userName'];
        $DS = DIRECTORY_SEPARATOR;
        $this->user_upload_path = APPPATH.'../../upload'.$DS.$_SESSION['profilepath'].$DS.$this->userFolder.$DS;

        $config['upload_path'] = $this->user_upload_path;
        $config['allowed_types'] = '*';
        $config['max_size']    = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';

        $data['title'] = "Transfer Upload";
        $data['heading'] = "File Transfer";
        $data['subHeading'] = "Upload Page";
        
        $error = array();

        $this->load->library('upload', $config);
        $this->header(1,$data);
        
        if ($_SESSION['profilepath'] != NULL)
        {

            if (!is_dir($this->user_upload_path))
            {
                mkdir($this->user_upload_path);
            }
            
            $data = array('upload_data' => $this->upload->data());
            
            if ((! $this->upload->do_upload()) && ($data['upload_data']['file_name'] != NULL))
            {
                $data = array('upload_data' => $this->upload->data());
                $file = $data['upload_data']['file_name']; // file name for logging
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_fail', $error);
            }
            if ((! $this->upload->do_upload()) && ($data['upload_data']['file_name'] == NULL))
            {
                $data = array('upload_data' => $this->upload->data());
                $file = $data['upload_data']['file_name']; // file name for logging
                $this->load->view('upload');
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());
                $file = $data['upload_data']['file_name']; // file name for logging
                $message = $user.' uploaded file '.$file .' to the upload share.';
                $this->load->view('upload_success', $data);
            }
            $this->footer();
        }
        else {
          $this->load->view('noProfilePath');
          $this->footer();
        }
    }

could someone help direct me on what I am doing wrong.

my view (in Firefox)shows that the current runtime is:html5 then shows the 2 links (select file) and (upload file)

select file shows the file selected under the current runtime display.
but when i click upload file i get the following error:

Quote:Current runtime: html5
empty file.txt (1 b)
Error: -200, Message: HTTP Error., File: empty file.txt

Also if i try to upload a file that is 1GB, i get the -600 error.

on IE, the Select File and Upload File links dont work at all.

With IE, is there anyway to get chunking to work for plupload while using CI?
#2

[eluser]Brennan Novak[/eluser]
Hi. I am using the same uploader plugin and have noticed the same error when uploading files larger than 2MB (even tho both my PLUploader and CI Upload allow that large of a file. My error is coming from somewhere in my custom image resizing library and is a 'bytes exhausted...' issue which is observable in my PHP logs
#3

[eluser]murphybr[/eluser]
instead of using PLupload, i used java upload.

all it took was 2 javascript files and my controller file + view to get an upload UI with the capability for single or multiple file uploads with the ability to upload (via chunking) files greater than 5GB.

view - upload.php
Code:
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;/head&gt;
[removed]
    
    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(2) + ", content=" + file.getResponseContent() );
    }
    /**
     * uploader status changed notification
     */
    function uploaderStatusChanged( uploader ) {
        traceEvent( "uploaderStatusChanged, status=" + uploader.getStatus() );
    }
[removed]

<div><b>&lt;?php echo "Max file upload limit: 1 file ";?&gt;</b>
</div><br>
&lt;applet name="jumpLoaderApplet"
        code="jmaster.jumploader.app.JumpLoaderApplet.class"
        archive="&lt;?php echo base_url()?&gt;javascript/jl_core_z.jar"
        width="715"
        height="500"
        mayscript&gt;
    <param name="uc_uploadUrl" value="&lt;?php echo base_url()?&gt;javascript/split_chunk.php"/>
    <param name="uc_partitionLength" value="1572864"/>
    <param name="uc_maxFiles" value="1"/>
    <param name="gc_loggingLevel" value="DEBUG"/>  
&lt;/applet&gt;
&lt;/html&gt;

controller - function placed in your main controller
Code:
function do_upload()
    {
        $DS = DIRECTORY_SEPARATOR;
        $this->user_upload_path = 'set/your/upload/path'
        
        $config['upload_path'] = $this->user_upload_path;
        $config['allowed_types'] = '*';
        $config['max_size']    = '0';
        $config['max_width']  = '0';
        $config['max_height']  = '0';

        $this->load->library('upload', $config);
            
        $data = array('upload_data' => $this->upload->data());
        $file = $data['upload_data']['file_name']; // file name for logging
        $this->load->view('upload');
    }

download the jl_core_z.jar file from the following site - http://jumploader.com/download.html and place it in the javascript folder.
#4

[eluser]murphybr[/eluser]
now the big piece under javascript folder create a file called split_chunk.php

Code:
&lt;?php
    //----------------------------------------------
    //    partitioned upload file handler script
    //----------------------------------------------

    //
    //    specify upload directory - storage
    //    for reconstructed uploaded files
    session_start();

    $DS = DIRECTORY_SEPARATOR;
    $upload_dir = 'path/to/upload/location';

    //
    //    specify stage directory - temporary storage
    //    for uploaded partitions
    $stage_dir = 'path/to/upload/location';
   //
    //    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 = $_SESSION['userName'].".".$file_name;

    //
    //    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 = $upload_dir . "importcsv.csv";
        $filePath = $upload_dir . $_POST[ 'fileName' ];
        
        if (file_exists($filePath))
        {
        //$file_ext = get_extension($_POST[ 'fileName' ]);
            $duplicate_filename = TRUE;
            $i=0;
            while ($duplicate_filename)
            {
                $filename_data = explode(".", $_POST[ 'fileName' ]);
                $new_filename = $filename_data[0] . "_" . $i . "." . $filename_data[1];
                $at = "$upload_dir".$new_filename."";
                if(file_exists($at))
                {
                    $i++;
                }
                else
                {
                    $duplicate_filename = FALSE;
                }
            }
            $file = $at;
        }
        else
        {
        $file = $upload_dir . $_POST[ 'fileName' ];
        }
        //unlink( $file );  

        $file_handle = fopen( $file, 'a' );
        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 );
         //  File is done the filename is $_POST[ 'fileName' ]
    }

?&gt;

AND with that I was able to implement the upload functionality in the view you can dictate how many files you want to download simultaneously with the following parameter:
Code:
<param name="uc_maxFiles" value="1"/>




Theme © iAndrew 2016 - Forum software by © MyBB