0

I am trying to make a stand-alone Flash/AIR up-loader. But I am having some problems. I am getting error that the file can't be uploaded. The script works fine with images as they are uploaded but gives me errors when it comes time to upload ZIP/RAR/Tar

Here is the Uploader.as

package {

    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    import flash.net.FileReference;
    import flash.net.FileReferenceList;
    import flash.net.FileFilter;
    import flash.net.URLRequest;
    import flash.utils.Timer;
    import flash.events.TimerEvent;


    public class Uploader extends MovieClip {

        var file:FileReference;
        var filefilters:Array;
        var req:URLRequest;
        var tm:Timer;
        var speed:Number = 0;
        var currbytes:Number = 0;
        var lastbytes:Number = 0;

        public function Uploader(){
            req = new URLRequest();
            req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : 'http://bios.website.ltd/client-000000000/upload.php';
            file = new FileReference();
            setup( file );
            select_btn.addEventListener( MouseEvent.CLICK, browse );
            progress_mc.bar.scaleX = 0;
            tm = new Timer( 1000 );
            tm.addEventListener( TimerEvent.TIMER, updateSpeed );
            cancel_btn.addEventListener( MouseEvent.CLICK, cancelUpload );
            cancel_btn.visible = false;
        }

        public function browse( e:MouseEvent ){
            filefilters = [ new FileFilter('Archives', '*.rar;*.zip;*.tar') ]; // add other file filters
            file.browse( filefilters );
        }

        private function setup( file:FileReference ){
            file.addEventListener( Event.CANCEL, cancel_func );
            file.addEventListener( Event.COMPLETE, complete_func );
            file.addEventListener( IOErrorEvent.IO_ERROR, io_error );
            file.addEventListener( Event.OPEN, open_func );
            file.addEventListener( ProgressEvent.PROGRESS, progress_func );
            file.addEventListener( Event.SELECT, selectHandler );
            file.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, show_message );      
        }

        private function cancel_func( e:Event ){
            trace( 'canceled !' );
        }

        private function complete_func( e:Event ){
            trace( 'complete !' );
        }

        private function io_error( e:IOErrorEvent ){
            var tf = new TextFormat();
            tf.color = 0xff0000;
            label_txt.defaultTextFormat = tf;
            label_txt.text = 'The file could not be uploaded.';
            tm.stop();
            cancel_btn.visible = false;
            select_btn.visible = true;
        }

        private function open_func( e:Event ){
            //trace( 'opened !' );
            tm.start();
            cancel_btn.visible = true;
            select_btn.visible = false;
        }

        private function progress_func( e:ProgressEvent ){
            progress_mc.bar.scaleX = e.bytesLoaded / e.bytesTotal;
            var tf = new TextFormat();
            tf.color = 0x000000;
            label_txt.defaultTextFormat = tf;
            label_txt.text = Math.round( (e.bytesLoaded/e.bytesTotal)*100)+'% uploaded '+speed+' kb/s';
            currbytes = e.bytesLoaded;
        }

        private function selectHandler( e:Event ){
            file.upload( req );

        }

        private function show_message( e:DataEvent ){
            tm.stop();
            var tf = new TextFormat();
            if( e.data == 'ok' ){
                tf.color = 0x009900;
                label_txt.defaultTextFormat = tf;
                label_txt.text = 'The file has been uploaded.';
            } else if( e.data == 'error'){
                tf.color = 0xff0000;
                label_txt.defaultTextFormat = tf;
                label_txt.text = 'The file could not be uploaded.';
            }
        }

        private function updateSpeed( e:TimerEvent ){
            speed = Math.round( (currbytes - lastbytes)/1024 );
            lastbytes = currbytes;
        }

        private function cancelUpload( e:MouseEvent ){
            file.cancel();
            reset();
        }

        private function reset(){
            cancel_btn.visible = false;
            select_btn.visible = true;
            label_txt.text = '';
            progress_mc.bar.scaleX = 0;
        }

    }   
} 

And here is the Upload.php

<?php
$uploads_dir = './data/';

if( $_FILES['Filedata']['error'] == 0 ){
    if( move_uploaded_file( $_FILES['Filedata']['tmp_name'], $uploads_dir.$_FILES['Filedata']['name'] ) ){
        echo 'ok';
        exit();
    }
}
echo 'error';
exit();
?>

Any clue or suggestion as to why I can't upload ZIP/RAR/Tar files?

Cl'
  • 1,535
  • 2
  • 10
  • 17
  • You might want to explain what errors you're seeing. It might help to identify where the problem actually is (for example, the PHP code vs. the Flash/AIR component). – GargantuChet Nov 13 '12 at 18:37
  • @GargantuChet actually it's Flash returning failed message as set in Uploader.as file "The file could not be uploaded" that's it. The PHP does not return any errors. – Cl' Nov 13 '12 at 18:39
  • You're not doing any checking for file upload errors. There's an error number stored in $_FILES that should be 0 if the uploaded succeeded. If it's not then the value corresponds to the error that occurred. – GordonM Nov 13 '12 at 20:26
  • 1
    That may help. The error is either being caught in "io_error" or "show_message". In either case I'd change "label_txt.text = 'The file could not be uploaded.';" to something like "label_txt.text = e.toString();". With luck you'll get a string that has more potential to explain why the process failed. – GargantuChet Nov 13 '12 at 20:27

0 Answers0