Connecting Tech Pros Worldwide Forums | Help | Site Map

What is wrong with this code?

Newbie
 
Join Date: Dec 2008
Posts: 27
#1: Jun 13 '09
Hi this one is hurtin' me!!

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $strID = $_GET["id"];
  3.  
  4. $filename = "c:\somedir\somedir\" . $strID . ".php";
  5. if (file_exists($filename)) {
  6.     $strIncPage = "somedir/" . $strID . ".php";
  7. } else {
  8.     $strIncPage = "somedir/error404.php";
  9. }
  10. ?>
Ig get the error

Expand|Select|Wrap|Line Numbers
  1. Parse error: parse error in C:\xampp\htdocs\index2.php on line (....x)
The problem persists with the "$filename" variable, I have tried jusing "'" characters instead of """ But then I get
Expand|Select|Wrap|Line Numbers
  1. Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\xampp\htdocs\index2.php on line 6
  2. c:\xampp\htdocs\insert_pages' . $strID . php 
I cant seem to find out when to use either "'" or """, and when and how to use "." in these statements...

Expand|Select|Wrap|Line Numbers
  1. $filename = 'c:\somedir\somedir\' . $strID . '.php';
Expand|Select|Wrap|Line Numbers
  1. $filename = 'c:\somedir\somedir\$strID.php';
Expand|Select|Wrap|Line Numbers
  1. $filename = "c:\somedir\somedir\$strID.php";
Frederik

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#2: Jun 13 '09

re: What is wrong with this code?


\ is used as escape character so if you have a certain combination (like \n which is the new line character) you end up having invalid characters in your file path.

EDIT: \" escapes the quotation mark, so that it is not treated as string closing character.
Newbie
 
Join Date: Dec 2008
Posts: 27
#3: Jun 13 '09

re: What is wrong with this code?


Thank you!

I edited the \ to / characters...
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#4: Jun 13 '09

re: What is wrong with this code?


1. what Dormilich mention about your invalid character that always you have to remenber in writing script.

2. you could put all the seperated part of the link in a variable and then call as a complate file name

Expand|Select|Wrap|Line Numbers
  1. if (file_exists($filename)) { 
  2.  
  3. $filename = "";
  4. $filename  .= "c:\somedir\somedir\";
  5. $filename  .= $strID;
  6. $filename  .= ".php";
  7. }else{
  8.  
  9. $filename  = somedir/error404.php";
  10. }
  11.  
:)
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#5: Jun 13 '09

re: What is wrong with this code?


Quote:

Originally Posted by prabirchoudhury View Post

Expand|Select|Wrap|Line Numbers
  1. $filename  .= "c:\somedir\somedir\";
  2.  
  3. $filename  = somedir/error404.php";

would result in a parse error.

EDIT: thank god I'm using posix (unix, linux, bsd, ...)
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#6: Jun 13 '09

re: What is wrong with this code?


Quote:

Originally Posted by prabirchoudhury View Post

1. what Dormilich mention about your invalid character that always you have to remenber in writing script.

2. you could put all the seperated part of the link in a variable and then call as a complate file name

Expand|Select|Wrap|Line Numbers
  1. if (file_exists($filename)) { 
  2.  
  3. $filename = "";
  4. $filename  .= "c:\somedir\somedir\";
  5. $filename  .= $strID;
  6. $filename  .= ".php";
  7. }else{
  8.  
  9. $filename  = somedir/error404.php";
  10. }
  11.  
:)

Line #4 in that code would fail, for the exact same reason the OP's original code failed.

And why would you test if the file path stored in $filename exists, and then construct it? Makes no sense.

Edit:
Dormilich beat me to it :)
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#7: Jun 13 '09

re: What is wrong with this code?


ya sry guys, you both right .. i have bn in illusion with \ and / ... it will brk on line 4..
Reply