What is wrong with this code? 
June 13th, 2009, 10:04 AM
| | Newbie | | Join Date: Dec 2008
Posts: 27
| |
Hi this one is hurtin' me!! - <?php
-
$strID = $_GET["id"];
-
-
$filename = "c:\somedir\somedir\" . $strID . ".php";
-
if (file_exists($filename)) {
-
$strIncPage = "somedir/" . $strID . ".php";
-
} else {
-
$strIncPage = "somedir/error404.php";
-
}
-
?>
Ig get the error - 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 - Warning: Unexpected character in input: ''' (ASCII=39) state=1 in C:\xampp\htdocs\index2.php on line 6
-
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... - $filename = 'c:\somedir\somedir\' . $strID . '.php';
- $filename = 'c:\somedir\somedir\$strID.php';
- $filename = "c:\somedir\somedir\$strID.php";
Frederik
| 
June 13th, 2009, 10:32 AM
|  | 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.
| 
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...
| 
June 13th, 2009, 11:00 AM
|  | 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 -
if (file_exists($filename)) {
-
-
$filename = "";
-
$filename .= "c:\somedir\somedir\";
-
$filename .= $strID;
-
$filename .= ".php";
-
}else{
-
-
$filename = somedir/error404.php";
-
}
-
:)
| 
June 13th, 2009, 12:15 PM
|  | 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 - $filename .= "c:\somedir\somedir\";
-
-
$filename = somedir/error404.php";
| would result in a parse error.
EDIT: thank god I'm using posix (unix, linux, bsd, ...)
| 
June 13th, 2009, 12:20 PM
|  | 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 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 -
if (file_exists($filename)) {
-
-
$filename = "";
-
$filename .= "c:\somedir\somedir\";
-
$filename .= $strID;
-
$filename .= ".php";
-
}else{
-
-
$filename = somedir/error404.php";
-
}
-
:) | 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 :)
| 
June 13th, 2009, 01:06 PM
|  | 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..
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|