0 Replies Latest reply on Jun 23, 2011 11:59 AM by zach.walker@williams.com

    Need help uploading a file using Flex and PHP

    zach.walker@williams.com

      I have a PHP script that I want to use to upload a file from a Flex app.  The php code works if I call it from an HTML page and the file is uploaded propperly.  When I try to use my Flex code I get a completed event but the file is no where to be found.  Here is the Flex and PHP code im using:

       

      <?PHP

      $target_path =

      "/";

      $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

      if

      (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

      echo "The file ". basename( $_FILES['uploadedfile']['name']).

      " has been uploaded";

      } else{

      echo "There was an error uploading the file, please try again!";

      }

      ?>

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application

      xmlns:fx="http://ns.adobe.com/mxml/2009"

      xmlns:s="library://ns.adobe.com/flex/spark"

      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">

      <fx:Script>

      <![CDATA[

      public var fileRef:FileReference;

      [Bindable]

      public var filename:String;

      [Bindable]

      public var fileSize:String;

      public function init():void

      {

      fileRef =

      new FileReference;

      fileRef.addEventListener(Event.SELECT, selectHandler);

      fileRef.addEventListener(Event.COMPLETE, completeHandler);

      }

      public function browseHandler(e:Event):void

      {

      fileRef.browse();

      }

      public function selectHandler(e:Event):void

      {

      uploadBtn.enabled =

      true;

      fileInfo.text = fileRef.name;

      fileSizeInfo.text = fileRef.size.toString();

      }

      public function completeHandler(e:Event):void

      {

      trace("Completed");

      }

      public function uploadHandler(e:Event):void

      {

      fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, uploadError);

      var url:String = 'http://127.0.0.1/uploader/uploader.php';

      var request:URLRequest = new URLRequest(url);

      var params:URLVariables = new URLVariables();

      params.enctype = "multipart/form-data";

      request.method = URLRequestMethod.POST;

      request.data = params;

      request.contentType = "multipart/form-data";

      fileRef.upload(request, "Custom1");

      }

      public function uploadError(e:Event):void

      {

      trace ("Upload Error");

      }

      ]]>

      </fx:Script>

      <fx:Declarations>

      <!-- Place non-visual elements (e.g., services, value objects) here -->

      </fx:Declarations>

      <s:Button id="browseBtn" x="443" y="299" label="Browse" click="browseHandler(event)"/>

      <s:Button id="uploadBtn" x="443" y="328" label="Upload" enabled="false" click="uploadHandler(event)"/>

      <s:TextInput id="fileInfo" x="413" y="269" editable="false"/>

      <s:Label x="340" y="273" text="File Name:"/>

      <s:TextInput id="fileSizeInfo" x="413" y="239" editable="false"/>

      <s:Label x="349" y="243" text="File Size:"/>

      </s:Application>