2 Replies Latest reply on Oct 8, 2011 11:02 AM by vivoices

    Uploading files to PHP

    vivoices Level 1

      Flash Builder 4.5

      SDK 4.5.1

       

      The PHP (the // Test part is to check if a script actually reached the php file):

      <?php
                $uploaddir = 'C:/Users/XXX/Desktop/UploadTest/';
                $uploadfilename = basename($_FILES['userfile']['name']);
                $uploadfile = $uploaddir . $uploadfilename;
      
      
                // Test
                $fhandle = fopen( $uploaddir . "wasHere.txt", "w" );
                fwrite ( $fhandle, implode(",", $_FILES['userfile']) );
                fclose( $fhandle );
                // End Test
      
      
                if( move_uploaded_file( $_FILES['userfile']['tmp_name'], $uploadfile ) ) {
                          echo( "status=success&file=" . $uploadfilename );
                } else {
                          echo( "status=error&file=" . $uploadfilename );
                }
      ?>
      

       

      The AS3:

      private function btnSendOrder_clickHandler( event:MouseEvent ):void
      {
                var req:URLRequest = new URLRequest( "http://ppupload.com/Up_Test.php" );
                req.method = URLRequestMethod.POST;
                var file:File = new File( "C:\Users\XXX\Pictures\141_small.jpg" );
                file.addEventListener( Event.COMPLETE, fileSent );
                file.upload( req );
      }
      
      
      private function fileSent( event:Event ):void
      {
        
      }
      

       

      When I upload a file to "Up_Test.php" via a regular HTTP form, the image ends up in the destination folder and the test file wasHere.txt is created as well.

       

      When uploading with the AS3 function above, only the wasHere.txt file is created, i. e. the AS3 connected to the PHP script, but it did not send the file.

       

      Any ideas what I might be missing?

       

       

      Thank you,

      Udo

        • 1. Re: Uploading files to PHP
          vivoices Level 1

          I modified the PHP to prevent the check that move_uploaded_file() performs:

          <?php
                    $uploaddir = 'C:/Users/XXX/Desktop/UploadTest/';
                    $uploadtempname = "C:/wamp/tmp/" . basename($_FILES['userfile']['tmp_name']);
                    $uploadfilename = basename($_FILES['userfile']['name']);
                    $uploadfile = $uploaddir . $uploadfilename;
          
          
                    // Test
                    $fhandle = fopen( $uploaddir . "wasHere.txt", "w" );
                    fwrite ( $fhandle, implode(",", $_FILES['userfile']) );
                    fclose( $fhandle );
                    // End Test
          
          
                    if( rename( $uploadtempname, $uploadfile ) ) {
                              echo( "status=success&file=" . $uploadfilename );
                    } else {
                              echo( "status=error&file=" . $uploadfilename );
                    }
          ?>
          

           

          and I changed the AS3 COMPLETE event:

          private function btnSendOrder_clickHandler( event:MouseEvent ):void
          {
                    var req:URLRequest = new URLRequest( "http://ppupload.com/Up_Test.php" );
                    req.method = URLRequestMethod.POST;
                    var file:File = new File( "C:\Users\XXX\Pictures\141_small.jpg" );
                    file.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, fileSent );
                    file.upload( req );
          }
          
          
          private function fileSent( event:DataEvent ):void
          {
            
          }
          

           

          But still no success with the upload.

          The "Filedata" array on the server shows the same file size, name, tmp_name as during an upload via html form, but the type is set to "application/octet-stream" when uploading from AS3 instead of "image/jpeg" via the html form.

           

          Any idea how to indicate the type in AS3?

          • 2. Re: Uploading files to PHP
            vivoices Level 1

            The property of the $_Files has to be whatever you set in AS3 file.upload( request, uploadDataFieldName ) as "uploadDataFieldName".

            The default value is "FileData".

             

             

            So changing $_FILES['userfile'] . . .   to  $_FILES['FileData'] . . . fixes the problem in case of the default value.