PDA

View Full Version : flash to php, writing to .txt file...


333944
11-09-2005, 03:59 PM
Hey Y'all,
Without trying to be redundant, I found this tutorial on flash-db.com (http://www.flash-db.com/Tutorials/saving/savingData.php?page=2). It outlines the steps on how to write to a text file through a php script actioned by your flash movie. The only problem is it doesn't work!!! After searching the forum I came across the thread entitled "php and flash, writing to text" (http://www.actionscript.org/forums/showthread.php3?t=53218&highlight=php+flash)... I modified the code from the original flash-db.com (http://http://www.flash-db.com/Tutorials/saving/savingData.php?page=2) site to resemble what CyanBlue had posted in reply to the original thread starter. My code now looks like this:

Flash MX 2004 (Flash Player 7 - Actionscript 1.0)
submit.onPress = function(){
myData = _root.myInput.text;
dirtyData1 = new LoadVars();
dirtyData1.myTextVar = _root.myInput.text;
dirtyData2.onLoad = function(success)
{
if (success)
{
//output loadVars worked
gotoAndStop(2)
status.text = "Submited data was saved"
}
else
{
//error
gotoAndStop(2)
status.text = "Error in saving submitted data"
}
};
dirtyData2.sendAndLoad("http://www.touchthedesign.com/listings05/saveListings.php", dirtyData1, "POST");
}

stop();



saveListings.php (Server Info (http://www.touchthedesign.com/info.php))
<?php
$listings = $_POST['Data1'];
$toSave = "Data1=" . $listings;

$fp = fopen("listings.txt", "w");

if (fwrite($fp, $toSave))
echo "writing=Ok";
else
echo "writing=Error";

fclose($fp);
?>


Both files were uploaded to my server, along with a blank tex tfile with nothing in it entitled "listings.txt" which I created. When I preview the html file online (http://www.touchthedesign.com/listings05/test1.html) and I click the button nothing happens, the browser just seems to hang...

I'm not sure what the problem is and I've been scratching my head for the past few days.... Please help someone!!!
Thank-You, Romeo

CyanBlue
11-09-2005, 05:50 PM
Howdy... :)

Can you post that sample file??? ;)

Flash Gordon
11-09-2005, 05:56 PM
My first guess would be you forgot "{" in you php code :

<?php
$listings = $_POST['Data1'];
$toSave = "Data1=" . $listings;

$fp = fopen("listings.txt", "w");

if (fwrite($fp, $toSave)) {
echo "&writing=Ok";
}else {
echo "&writing=Error";
}
fclose($fp);
?>

Upload that then go straight to the PHP page and you should see a respone. If you don't get anything, there is still something wrong in the php code.

333944
11-09-2005, 06:17 PM
Flash,
Smooth move on my part, can’t believe I forgot the braces :p...
I've re-uploaded the php file and when I view it through my browser I get this:

Warning: fopen(listings.txt): failed to open stream: Permission denied in /home/virtual/site48/fst/var/www/html/listings05/saveListings.php on line 5

Warning: fwrite(): supplied argument is not a valid stream resource in /home/virtual/site48/fst/var/www/html/listings05/saveListings.php on line 7
writing=Error
Warning: fclose(): supplied argument is not a valid stream resource in /home/virtual/site48/fst/var/www/html/listings05/saveListings.php on line 12

... And I have no clue what that means. Is their a problem with my server???

Cyan Blue, Which sample file did you want me to post???

Keep on contact, Romeo

CyanBlue
11-09-2005, 07:13 PM
You don't realy have to color code your response... ;)

I meant the FLA and PHP file that you have uploaded in your host...

To me, the error message has to do with the file permission of your text file...

Try using the CHMOD to make it writable... I think 644 should do it...

333944
11-09-2005, 07:24 PM
Cyan Blue,
Here are the files: (I included all three)
Keep on contact, Romeo
Ps. Not sure I know what you mean about the CHMOD, 644...

333944
11-09-2005, 10:25 PM
Hey CyanBlue,
I changed the CHMOD permission's through my ftp client of that file to 666.
Now when I check the actual php file through my browser (Thanks to Flash Gordon ;) ) the page reads "writing=Ok", and the listings.txt file, once downloaded and opened reads "Data1="... Everything seems ok!

The only problem is when I try and update the txt file through the flash file via the browser... it still hangs, leading me to believe that there's something wrong with the flash file????
Please Help....

Keep on contact, Romeo

CyanBlue
11-09-2005, 10:40 PM
This is your PHP script which is looking for a POST variable called 'Data1'...
$listings = $_POST['Data1'];
But you NEVER defined 'Data1' in Flash...

Change this line...
dirtyData1.myTextVar = _root.myInput.text;
to this...
dirtyData1.Data1 = _root.myInput.text;

Flash Gordon
11-09-2005, 10:43 PM
copy and paste this exactly as is

var dirtyData1 = new LoadVars();
dirtyData1.onLoad = function(success) {
if (success) {
//output loadVars worked
gotoAndStop(2);
status.text = "Submited data was saved";
} else {
//error
gotoAndStop(2);
status.text = "Error in saving submitted data";
}
};
submit.onPress = function() {
dirtyData1.myTextVar = _root.myInput.text;
trace(dirtyData1);
dirtyData1.sendAndLoad("http://www.touchthedesign.com/listings05/saveListings.php", dirtyData1, "POST");
};
stop();


EDIT:: this fixes the flash hang. CyanBlue fixed it to before I posted. Go with what ever he says, I am just a peon compared to his knowledge.

333944
11-10-2005, 05:06 PM
Hey Guys,
Thanks for the ongoing support, but still no luck with this :(
Here's my new Flash Code, as posted by Flash Gordon:

var dirtyData1 = new LoadVars();
dirtyData1.onLoad = function(success) {
if (success) {
//output loadVars worked
gotoAndStop(2);
status.text = "Submited data was saved";
} else {
//error
gotoAndStop(2);
status.text = "Error in saving submitted data";
}
};
submit.onPress = function() {
dirtyData1.myTextVar = _root.myInput.text;
trace(dirtyData1);
dirtyData1.sendAndLoad("http://www.touchthedesign.com/listings05/saveListings.php", dirtyData1, "POST");
};
stop();
... And here's my new php file, as posted by CyanBlue (with a small change to the variable being passed; instead of Data1, I believe it should be dirtyData1 as per Flash Gordon's script):
<?php
$listings = $_POST['dirtyData1'];
$toSave = "Data1=" . $listings;

$fp = fopen("listings.txt", "w");

if (fwrite($fp, $toSave)) {
echo "writing=Ok";
}else {
echo "writing=Error";
}
fclose($fp);
?>

With the new Actionscript the movie no longer hangs on the first frame... Thanks Flash Gordon!!! But the textfile gets none of the text content passed to it.
The only thing that gets sent is "Data1=", I tried using 'GET' instead of 'POST', and that didn't seem to add to the URL at all... Not sure what's goin' on but it's really messing me up...
Thank-you, Romeo

CyanBlue
11-10-2005, 05:14 PM
As I was saying earlier, your PHP is looking for a variable called 'dirtyData1' yet Flash code is sending out 'myTextVar'... What you have AFTER the dot should match not the one before... ;)
dirtyData1.myTextVar = _root.myInput.text;
$listings = $_POST['myTextVar'];

333944
11-10-2005, 05:23 PM
Cyan Blue!!!!!!
It works :D .... As I figured it out and was going to post back, when I saw you had already clarified (the fastest you've been ;) )...

I can't thank you and Flash Gordon enough...
Thank-You very very very very .... very much Romeo

333944
11-10-2005, 06:39 PM
Hey Guys,
Sorry to re-open this but I need to know one more thing before I go. :confused:

If I wanted to update the textfile rather than overwrite it ???

I tried changing this line in the php file:
$fp = fopen("listings.txt", "w");
to this:
$fp = fopen("listings.txt", "a+");
... But that doesn't seem do it, Is there something else I'm missing???
Keep on contact, Romeo

Flash Gordon
11-11-2005, 05:46 PM
In words read the php first, send that to flash, then overwrite it with new date plus the old one you sent to flash

EX. txt: 8 -->Flash 8+1 --> file 9.
EX2. If it is just numbers.

num += $_POST['flashLV'];

aktivekarma
12-02-2005, 01:57 PM
I have looked over this thread about a hundred others in a number of forums throughout the net on this problem. I think I have set up the PHP code and actionscript correctly based on all of the information that I have gotten to date, but I still keep getting the permissions error on the PHP page. The server is running PHP v.4.3.10 and I have changed the permissions on the file to rw-rw-rw for the .txt file?

What am I doing wrong? I am not real familiar with PHP so I can post code and actionscript if that helps.

::LD::

CyanBlue
12-02-2005, 02:22 PM
Howdy and Welcome...

What's the error message like???

aktivekarma
12-02-2005, 02:57 PM
Thanks for the response CyanBlue, I am new to the forum as you noticed thank you for the Welcome! I am postin the error message and PHP code below. Any help is great, I have been racking my head for a couple of days.

Heres the browser error message:

Warning: fopen(\home4\ipop\www\exttxt\pages\home.txt): failed to open stream: Permission denied in /home4/ipop/ipoponline-www/updateHome.php on line 8

Warning: fwrite(): supplied argument is not a valid stream resource in /home4/ipop/ipoponline-www/updateHome.php on line 12
The server has denied you acces to this file at this time!

Warning: fclose(): supplied argument is not a valid stream resource in /home4/ipop/ipoponline-www/updateHome.php on line 23

And here's the PHP code:
<?php

// Set all the variables

$fileName = "\home4\ipop\www\exttxt\pages\home.html";
$newText = $_POST['input'];

$writeFile = fopen("$fileName", "w");

// Write the file to the server

if (fwrite($writefile, $newText)) {
echo "The file was successfully written to the server!";

// Server response

} else {
echo "The server has denied you acces to this file at this time!";
}

// Close the PHP file

fclose($writeFile);

?>
Cheers,

:: LD ::

aktivekarma
12-02-2005, 09:30 PM
// import the text file to edit

output.html = false;

var loadText:LoadVars = new LoadVars();

loadText.onData = function(contents) {
output.text = contents;
}

loadText.load("url for text to load");

// Setup the variables for the PHP file to write

var inputText:LoadVars = new LoadVars();

// Send the variables to the PHP page and get return values

inputText.onLoad = function(success) {
if (success) {
// set "SUCCESS" output
gotoAndStop("home-success");
phpReply.text = "Your new page has been successfully saved, pleaes verify text below.";
} else {
// set the failure return values
phpReply.text = "Your page failed to save, please try again!";
}
};

// Perform the button actions

submit.onPress = function() {
inputText.postToPHP = _root.textInput.text;
trace(inputText);
inputText.seandAndLoad("url of php page", postToPHP, "POST");
};

stop();

<?php

// Set all the variables

$fileName = "absolute path of the document";
$newText = $_POST['postToPHP'];

$writeFile = fopen("$fileName", "w");

// Write the file to the server

if (fwrite($writefile, $newText)) {
echo "The file was successfully written to the server!";

// Server response

} else {
echo "The server has denied you acces to this file at this time!";
}

// Close the PHP file

fclose($writeFile);

?>

CyanBlue
12-02-2005, 09:43 PM
Well... Let's do it one by one...

The PHP error message is saying that the file is not there so PHP cannot save anything... Try this script and we can go from there...
<?php

// Set all the variables

$fileName = "absolute path of the document";

if (!file_exists($fileName))
{
fopen($fileName, 'w+');
fclose($fileName);
chmod($fileName, 0777);
}

$newText = $_POST['postToPHP'];

$writeFile = fopen("$fileName", "w");

// Write the file to the server

if (fwrite($writefile, $newText)) {
echo "The file was successfully written to the server!";

// Server response

} else {
echo "The server has denied you acces to this file at this time!";
}

// Close the PHP file

fclose($writeFile);

?>

aktivekarma
12-03-2005, 03:47 PM
Tried the new code and got the followinng error message:

Fatal error: Call to undefined function: fopen() in /home4/ipop/ipoponline-www/exttxt/pages/_backend/updateHome.php on line 9

I checked the server PHP information and the info said that file write premissions were currently on???

I have no idea what's going on with it. The flash interface is passing the variables correctly now, becasue the interface kind of hickupped (sp?) when I pressed the submit button.

Thank you again for your help, if I figure something out I will post in case some else runs into this same kind of problem.

CyanBlue
12-03-2005, 04:01 PM
Huh, undefined function for fopen()??? I am not quite understanding it... That's a genuine PHP function which should not be undefined... Hm...

What's the name of your host???
What's the PHP version they support???
You changed the value of $fileName from "absolute path of the document" to something else, right???

Can you 'upload' the FLA/SWF/PHP files??? I want to test it on my server to see if it happens on my server as well... If you don't mind that is...

aktivekarma
12-03-2005, 05:16 PM
I chagned the path, it is supposed to be the absolute server path not a url path, correct. Because the return error reads as a url path not absolute path. If I understand correctly, I though for the file to open you have to give it a directory path like in Windows Explorer, and not a url like in IE. Maybe that is where I am messing up the code. I will post the files later on today, no with me right now.

Thank you again for your help.

aktivekarma
12-05-2005, 05:51 PM
I put all of the files that I am using to try and update the test file with flash in the zip document. Sorry about the delay and thank you again for the help.

Flash Gordon
12-06-2005, 02:14 AM
I would chane this line $fileName = "absolute path of server"; to this
$fileName = getcwd() . '/';

aktivekarma
12-06-2005, 02:17 AM
Flash,

Stupid question in the () tag of the getcwd do I put the path to the file I am update, url or nothing???

Thanks in advance: LD

Flash Gordon
12-06-2005, 02:20 AM
http://us3.php.net/manual/en/function.getcwd.php

aktivekarma
12-06-2005, 02:23 AM
cool, thank you for the link: Cheers