Error: Cannot get PHP script to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jav23D
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 10:27 am

Error: Cannot get PHP script to work

Post 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.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Error: Cannot get PHP script to work

Post 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.
Jav23D
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 10:27 am

Re: Error: Cannot get PHP script to work

Post 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?
Jav23D
Forum Newbie
Posts: 3
Joined: Mon Aug 31, 2009 10:27 am

Re: Error: Cannot get PHP script to work

Post 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.
Post Reply