File upload using Flash Action Script 3 without PHP

Discussion in 'Programming' started by transness, May 31, 2009.

  1. #1
    Hi,

    I need the code for AS3 to upload files... It shouldn't use PHP.

    Thanks
     
    transness, May 31, 2009 IP
  2. onpub

    onpub Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I had to do a lot of searching and testing to figure out a way to allow users to upload images on the word associator, turns out the AS2 was is completely out as it's been replaced with a much easier method using file reference, but since there's not a lot of documentation out about it I thought I'd post my code if anyone else is having trouble searching for it as well.

    First: define variables and import FileReference

    import flash.net.FileReference;
    flash.system.Security.allowDomain("http://localhost/");
    var imageTypes:FileFilter = new FileFilter("Images (*.jpg)", "*.jpg"); // allow only jpg
    var allTypes:Array = new Array(imageTypes);
    var fileRef:FileReference = new FileReference();


    Next, after clicking an upload button we need to open the browse box on the user's machine

    function browseImages(e:MouseEvent) {
    fileRef.addEventListener(Event.SELECT, selectHandler);
    fileRef.browse(allTypes);
    function selectHandler(e:Event) {
    imageSelected = true;
    myTextBox.text = fileRef.name;
    // use the function to store a variable to let flash know you've selected an image
    // it's also a good idea to have a text display of the filename the user selected
    }
    }


    After clicking a submit button the function for calling the php uploader goes something like this:

    function uploadImage() {
    if(fileRef.size > 300000) {
    trace("image size over 300kb");
    } else {
    var requestF:URLRequest = new URLRequest("http://www.yoursite.com/upload.php");
    fileRef.upload(requestF);
    fileRef.addEventListener(Event.COMPLETE, fileDone);
    function fileDone(e:Event) {
    sendWords("s", imagePath);
    // use this to call another function to store the image path in a database if you want
    }
    }
    }


    Now for the PHP script, you will also need to set up 2 folders in your web directory "temporary" and "uimages".

    $MAXIMUM_FILESIZE = 1024 * 300; // 300KB
    $MAXIMUM_FILE_COUNT = 10; // keep maximum 10 files on server
    echo exif_imagetype($_FILES['Filedata']);
    if ($_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE)
    {
    move_uploaded_file($_FILES['Filedata']['tmp_name'], "./temporary/".$_FILES['Filedata']['name']);
    $type = exif_imagetype("./temporary/".$_FILES['Filedata']['name']);
    if ($type == 1 || $type == 2 || $type == 3)
    {
    rename("./temporary/".$_FILES['Filedata']['name'], "./uimages/".$_FILES['Filedata']['name']);
    }
    else
    {
    unlink("./temporary/".$_FILES['Filedata']['name']);
    }
    }
    $directory = opendir('./uimages/');
    $files = array();
    while ($file = readdir($directory))
    {
    array_push($files, array('./uimages/'.$file, filectime('./uimages/'.$file)));
    }
    usort($files, sorter);
    if (count($files) > $MAXIMUM_FILE_COUNT)
    {
    $files_to_delete = array_splice($files, 0, count($files) - $MAXIMUM_FILE_COUNT);
    for ($i = 0; $i < count($files_to_delete); $i++)
    {
    unlink($files_to_delete[$i][0]);
    }
    }
    print_r($files);
    closedir($directory);

    function sorter($a, $b)
    {
    if ($a[1] == $b[1]) { return 0; }
    else { return ($a[1] < $b[1]) ? -1 : 1; }
    }
    ?>
     
    onpub, Jun 1, 2009 IP