Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

From AS3 write STRING to txt file with PHP

Explorer ,
Apr 16, 2018 Apr 16, 2018

I have problem.

My AS3 code:

submit_btn.addEventListener(MouseEvent.CLICK, writeLine);

function writeLine(e:MouseEvent):void{

    var my_vars:URLVariables = new URLVariables();

    my_vars.kazkas1_t = kazkas1.text;

    my_vars.kazkas2_t = kazkas2.text;

    my_vars.kazkas3_t = kazkas3.text;

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = my_vars;

    var my_loader:URLLoader = new URLLoader();

    my_loader.dataFormat = URLLoaderDataFormat.VARIABLES;

    my_loader.load(my_url);

    kazkas3.text = "IÅ¡saugota";

}

and PHP code:

<?php

$myfile = fopen("test.txt", "a")

$current = ($_POST['kazkas1_t'] . " " . $_POST['kazkas2_t'] . $_POST['kazkas3_t'] .'\n');

fwrite($current .'\n');

?>

But i GET error:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

    at Error$/throwError()

    at flash.net::URLVariables/decode()

    at flash.net::URLVariables()

    at flash.net::URLLoader/onComplete()

WHAT IM DOING WRONG?

TOPICS
ActionScript
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Apr 17, 2018 Apr 17, 2018

correct answer:

function writeLine(e:MouseEvent):void{

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = "kazkas1_t="+kazkas1.text+"&kazkas2_t="+kazkas2.text+"&kazkas3_t="+kazkas3.text;

    var my_loader:URLLoader = new URLLoader();   try {       my_loader.load(my_url);   } catch (error:Error) {       trace("Unable to load requested document.");   }

}

private function configureListeners(dispatcher:IEventDi

...
Translate
Advisor ,
Apr 16, 2018 Apr 16, 2018

do you manage COMPLETE loader event?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2018 Apr 16, 2018

Hm these scrips is all what I have.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 16, 2018 Apr 16, 2018

try this and tell me if it works. Also use HTTPS instead of HTTP in production mode or anyone can catch your data on the fly

function writeLine(e:MouseEvent):void{

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = "kazkas1_t="+kazkas1.text+"&kazkas2_t="+kazkas2.text+"&kazkas3_t="+kazkas3.text;

    var my_loader:URLLoader = new URLLoader();   try {       my_loader.load(my_url);   } catch (error:Error) {       trace("Unable to load requested document.");   }

}

private function configureListeners(dispatcher:IEventDispatcher��void {  dispatcher.addEventListener(Event.COMPLETE, completeHandler);  dispatcher.addEventListener(Event.OPEN, openHandler);  dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);  dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);  dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);  dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);}private function completeHandler(event:Event��void {   var loader:URLLoader = URLLoader(event.target);  trace("completeHandler: " + loader.data);}private function openHandler(event:Event��void {  trace("openHandler: " + event);}private function progressHandler(event:ProgressEvent��void {  trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);}private function securityErrorHandler(event:SecurityErrorEvent��void {  trace("securityErrorHandler: " + event);}private function httpStatusHandler(event:HTTPStatusEvent��void {  trace("httpStatusHandler: " + event);}private function ioErrorHandler(event:IOErrorEvent��void {  trace("ioErrorHandler: " + event);}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

No errors, but i dont get any string to my txt. Maby its php code wrong.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

Or maby is the way to do the same without php just with as3 in AIR?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

in your php check permission of the file you are opening.

must be allowed to write with php system user (usually nobody or www)

<?php

$fp =@ fopen("test.txt", "a") ;

if($fp){

     $current = ($_POST['kazkas1_t'] . " " . $_POST['kazkas2_t'] . $_POST['kazkas3_t'] .'\n');

     fwrite($current);

     fclose($fp);

}else{

     // answer to flash by something like varNamer=error to tell the flash client that something wrong happen

    // or send and email to yourself to debug it

}

?>

AIR is something else, depend what you'd like to achieve, be clear of what you need thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

OMG im so tired of that...

I have the same... No errors but my txt file is empty.

test.txt permisions is 777

I just need to write string from 3 textfields in to test.txt file and every time in the new line.

But 2 days im looking for simple thing.

I tried something like this:

import flash.fileSystem.*;

import flash.utils.ByteArray;

import flash.events.Event;

var file:File =  new File("test.txt");

    var stream:FileStream = new FileStream();

    stream.open(file, FileMode.WRITE);

    stream.writeUTFBytes("Hello World");

    stream.close();

But its like madhouse. 10000 erors

Scene 1, Layer 'Layer 1', Frame 1, Line 1, Column 24    1172: Definition flash.fileSystem could not be found.

Scene 1, Layer 'Layer 1', Frame 1, Line 7, Column 10    1046: Type was not found or was not a compile-time constant: File.

Scene 1, Layer 'Layer 1', Frame 1, Line 8, Column 16    1046: Type was not found or was not a compile-time constant: FileStream.

Scene 1, Layer 'Layer 1', Frame 1, Line 7, Column 22    1180: Call to a possibly undefined method File.

Scene 1, Layer 'Layer 1', Frame 1, Line 8, Column 33    1180: Call to a possibly undefined method FileStream.

Scene 1, Layer 'Layer 1', Frame 1, Line 9, Column 23    1120: Access of undefined property FileMode.

I added all library paths but seems it not helped. AIRGLOBAL.SWC AIR 2,5;  AIRGLOBAL.SWC AIR 23,0;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

?Really thanks for your time and help but its not working:|

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

what's your application? AIR or Flash?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

AIR aplication 😕

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

To debug it, check your php script to see if you receive the POST

like send an email to yourself with print_r($_POST,true) as body message

also the path of the text file you are opening with fopen must be absolute (with AIR it must start with file:// if local if I remember)

there is nothing complicated in what you wan to do.

the thing is you must find a easy way to debug your code from any step or you will lose a lot of time for nothing

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

Also, be aware of the sandbox violation, not remember how AIR manage it compared to flash

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

your AIR example does something different, it opens a LOCAL file from the client,

but your php script is on SERVER, so the text file must be on the server as well

and to write on it you must use an absolute path

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

on server app folder i have id.php and test.txt file. txt permisions 777. so why i gantb get string in text file. Seems it have to work

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

in email i get string it working good. But why i cant write string to txt file i dont understand

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

change also into the code I wrote

fwrite($current);

with

if(@fwrite($current)){

    // ok

}else{

     // debug issue

}

fclose($fp);

where is your text file?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

[17-Apr-2018 11:15:56 UTC] PHP Parse error:  syntax error, unexpected '$_POST' (T_VARIABLE) in /home/mesmylimbirzus/public_html/app/id.php on line 5

i have error log file

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

depend many things since your text file is in the same folder of your PHP script which is a very serious security issue

- the web server does not allow to write to any file as long as it is exposed to public

- the folder containing your text file does not allow any execution

my advice.

move the text file out of the web root, somethin like /home/myData/blabla

make blabla own by the web server user (check your web server configuration)

you don't need 777 as long as the text file is own by the right user so the web server system user with 600 permissions

use absolute path to open it

fopen("/home/myData/blabla/myfile.txt","a");

I supposed you use "a" to append each line of text right?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

I dont understand what you just said:DDD

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

PHP must finish always with a semicolon like ";"

if not php throw an error.

your php scrip has a code error, correct it and test again

and try your script URL directly with a browser to see if no error

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

so far so good, always check if any error from the web server log.

I just saw something that makes no sense

change

$current = ($_POST['kazkas1_t'] . " " . $_POST['kazkas2_t'] . $_POST['kazkas3_t'] .'\n');

to

$current = $_POST['kazkas1_t'] . $_POST['kazkas2_t'] . $_POST['kazkas3_t'].'\n';

always use the PHP cli from a terminal  to be sure there is no PHP error

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

check the semi colons too!!!

$myfile = fopen("test.txt", "a")

to

$myfile = fopen("test.txt", "a"); <- !!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2018 Apr 17, 2018

My AIR code looks like this now:

submit_btn.addEventListener(MouseEvent.CLICK, writeLine);

function writeLine(e:MouseEvent):void{

    var my_url:URLRequest = new URLRequest("http://www.mesmylimbirzus.lt/app222/id.php");

    my_url.method = URLRequestMethod.POST;

    my_url.data = "kazkas1_t="+kazkas1.text+"&kazkas2_t="+kazkas2.text+"&kazkas3_t="+kazkas3.text;

      var my_loader:URLLoader = new URLLoader();

   try {

      my_loader.load(my_url);

} catch (error:Error) {

       trace("Unable to load requested document.");

   }

}

my php:

<?php

$fp =@ fopen("test.txt", "a");

if($fp){

  $current = $_POST['kazkas1_t'] . " " . $_POST['kazkas2_t'] . $_POST['kazkas3_t'] .'\n';

  if(@fwrite($current)){

    // ok

}else{

     // debug issue

}

fclose($fp);

}else{

     // answer to flash by something like varNamer=error to tell the flash client that something wrong happen

    // or send and email to yourself to debug it

}

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 17, 2018 Apr 17, 2018

2 errors:

change

$fp =@ fopen("test.txt", "a");

to

$fp = @fopen("test.txt", "a");

change

if(@fwrite($current)){

to

if(@fwrite($fp,$current)){

and once it works use another path for your text file since everyone can write on it from the internet

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines