PHP Help Variables

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
IamTK
Forum Newbie
Posts: 7
Joined: Tue Aug 28, 2007 11:16 am

PHP Help Variables

Post by IamTK »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can someone tell me what's wrong with this code?

Code: Select all

$nhandle = fopen("upload/" . $_POST['docname'], a+");
$nstring = $_POST["uploaddoc"]; *Specifically, this line*
fwrite($nhandle, $nstring);
fclose($nhandle);
The error is

Parse error: syntax error, unexpected T_VARIABLE


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

This code contain 2 syntax errors and 1 security error.
IamTK
Forum Newbie
Posts: 7
Joined: Tue Aug 28, 2007 11:16 am

Post by IamTK »

xpgeek wrote:This code contain 2 syntax errors and 1 security error.
can you tell me where?

and how to fix it?
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

Do you learn php or do you use it in copy paste mode ? :wink:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: PHP Help Variables

Post by superdezign »

IamTK wrote:$nhandle = fopen("upload/" . $_POST['docname'], a+");

[...]

Parse error: syntax error, unexpected T_VARIABLE
That line is the culprit. What is 'a' and why do you try to perform an addition operation on it and an empty string?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What is 'a' and why do you try to perform an addition operation on it
read about fopen() modes
IamTK
Forum Newbie
Posts: 7
Joined: Tue Aug 28, 2007 11:16 am

Re: PHP Help Variables

Post by IamTK »

superdezign wrote:
IamTK wrote:$nhandle = fopen("upload/" . $_POST['docname'], a+");

[...]

Parse error: syntax error, unexpected T_VARIABLE
That line is the culprit. What is 'a' and why do you try to perform an addition operation on it and an empty string?
a+ moves the pointer to the last character and + makes it writable. It means, write the file at the last character. And yes, I do learn PHP myself.
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Re: PHP Help Variables

Post by Steve Mellor »

IamTK wrote:

Code: Select all

$nhandle = fopen("upload/" . $_POST['docname'], a+");
$nstring = $_POST["uploaddoc"]; *Specifically, this line*
fwrite($nhandle, $nstring);
fclose($nhandle);
OK, since no one else seams to want to help. You said the problem is with the line:

Code: Select all

$nstring = $_POST["uploaddoc"];
Well it isn't. This often happens in PHP with error codes. It experiences an error on the line after a mistake in the code, usually where a string isn't closed or there's a missing semi-colon.

Well, in your fopen statement you've got:

Code: Select all

$_POST['docname'], a+");
The a+" should be "a+" which means PHP is treating the rest of your script as a string, until it reaches the next ".

Also, you've got two security errors in your script. Namely you're using POST variables directly in your script. Process the variables first, make sure the data that is getting processed is the data you want to be processed or you're likely to get 'spam'.
Post Reply