Page 1 of 1

Error: Cannot get PHP script to work

Posted: Mon Aug 31, 2009 10:52 am
by Jav23D
I am trying to enable a flash file to post some data to a PHP file in order to edit an existing XML file. The PHP manages to edit the XML file when the file location is written in the PHP script itself, but i need the file location to be posted by the flash file and then found by the PHP file from there. Here is a code which i tried to get working, but have been getting problems in getting the PHP script to find the file from the variables posted by the flash file.

Code: Select all

 
 
if (isset( $_POST ))
   $postArray = &$_POST ;
else
   $postArray = &$HTTP_POST_VARS;
 
$user = $postArray[theuser];
$log = $postArray[thelog];
$back = $postArray[theback];
 
$fname = $user;
 
$arr = array(
 
"New XML Data"
 
        );
 
 
$nfile = fopen($fname, "w");
foreach($arr as $x)
{
        fwrite($nfile, $x);
}
fclose($nfile);
 
 
The code above returned repeated errors stating:
'Warning: fwrite(): supplied argument is not a valid stream resource in edit.php on line 69'
AND
'Warning: fclose(): supplied argument is not a valid stream resource in edit.php on line 71'

Line 69 refers to 'fwrite($nfile, $x);' and line 71 refers to 'fclose($nfile);'.

I tried to change '$fname = $user;' to '$fname = array($user);', but that returned the error:
'Warning: fopen() expects parameter 1 to be string, array given in edit.php on line 66'

Line 66 refers to '$nfile = fopen($fname, "w");'.

At first, '$fname' was '$fname = "example.xml";' which worked perfectly.

I don't have much knowledge in PHP and therefore have no idea on how i can convert the variable $user into a string, which i think is required.

Any kind of help is much appreciated.

Re: Error: Cannot get PHP script to work

Posted: Mon Aug 31, 2009 1:10 pm
by cpetercarter
It may just be a mistake in transcribing the code for entry into the forum, but this bit:

Code: Select all

 
 $user = $postArray[theuser];
 $log = $postArray[thelog];
 $back = $postArray[theback];
 
should be

Code: Select all

 
 $user = $postArray['theuser'];
 $log = $postArray['thelog'];
 $back = $postArray['theback'];
 
If that doesn't help, do a dump of $_POST and see if the various elements are what you expect them to be.

Re: Error: Cannot get PHP script to work

Posted: Tue Sep 01, 2009 5:27 am
by Jav23D
Thanks for the reply, but still no luck.

I tried adding the apostrophes, but there was no change, and $_POST works only when '$fname = "example.xml";' is given, but not when' $fname = $user;' is used.

Is there any way to make PHP treat the variable as if it were written in the PHP script?

Re: Error: Cannot get PHP script to work

Posted: Tue Sep 01, 2009 6:05 am
by Jav23D
I have managed to find a solution to this problem:

Instead of posting the 'theuser' variable from Flash to PHP, I decided to switch the PHP script to $_GET the 'theuser' variable, and used flash to submit the data to 'example.php?theuser=example.xml', and decided to just get Flash to edit the file location in the URL every time it posts to the PHP file. Works perfectly.