Connecting Tech Pros Worldwide Help | Site Map

What is wrong with this code?

  #1  
Old June 13th, 2009, 10:04 AM
Newbie
 
Join Date: Dec 2008
Posts: 27
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
  #2  
Old June 13th, 2009, 10:32 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

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.
  #3  
Old June 13th, 2009, 10:54 AM
Newbie
 
Join Date: Dec 2008
Posts: 27

re: What is wrong with this code?


Thank you!

I edited the \ to / characters...
  #4  
Old June 13th, 2009, 11:00 AM
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152

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.  
:)
  #5  
Old June 13th, 2009, 12:15 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,487
Provided Answers: 9

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, ...)
  #6  
Old June 13th, 2009, 12:20 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

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 :)
  #7  
Old June 13th, 2009, 01:06 PM
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is wrong with this code? Bill Reid answers 30 July 1st, 2007 02:05 AM
What is wrong with this code to populate a DropDownList? Rui Macdonald answers 2 November 19th, 2005 09:55 AM
what is wrong with this code ? hi-q Niklaus answers 6 November 14th, 2005 08:10 AM
What is wrong with this code? datastructure answers 9 July 22nd, 2005 07:09 AM
What is wrong with this code? siu02rk answers 10 July 17th, 2005 09:29 AM