php include and file() issue

A

Anonymous

Guest
HI All, Having some problems using php include. I have a page that has a case statement in it although the various case's are contained in a seperate txt file. I can't seem to include them in my page.

Code is like this.

switch($gallery){
file("details.txt");


and the txt file looks like this:

case "dad":
$_pwd="dad";
$gallery="../pics/dad/";
$gname="dad";
$user="viewer";
$email="dad@comports.net";
break;
case "admindad":
$_pwd="admindad";
$uname="dad";
$gname="dad";
$gallery="../pics/dad/";
$user="admin";
$email="dad@comports.net";
break;

case "jessica":
$_pwd="jessica";
$gallery="../pics/jessica/";
$gname="jessica";
$user="viewer";
$email="jessica@comports.net";
break;
case "adminjessica":
$_pwd="adminjessica";
$uname="jessica";
$gname="jessica";
$gallery="../pics/jessica/";
$user="admin";
$email="jessica@comports.net";
break;


I have done it like this as I can write to and update the text file automatically without having to re code the php page everytime I add a new gallery. Problem is the txt file is not rendered into my php and gives an error. Any clues..?

Thanks
Ash
 
You can't do that!!! Think of some more logical model.

It's working code nearest to your pseudo-model:
Code:
eval("switch('$gallery'){" . file_get_contents('details.txt') . "}");

But I strictly suggest NOT to use it. It's unsafe (function eval) and not usable.

P.S. Why don't you write switch inside txt file and include it by function include/ require???
 
GedroX said:
You can't do that!!! Think of some more logical model.

It's working code nearest to your pseudo-model:
Code:
eval("switch('$gallery'){" . file_get_contents('details.txt') . "}");

But I strictly suggest NOT to use it. It's unsafe (function eval) and not usable.

P.S. Why don't you write switch inside txt file and include it by function include/ require???

Thanks for that but now I'm totally lost. I write (append) to the text file each time I add a new gallery. It writes the password and email address of the gallery to the text file so that when you next log in to the system it knows (by reading the case statement options) which gallery to load and what permissions you should have. Otherwise I have to manually add these details to the php page and upload it again. All I want is for the text in the .txt file to appear as code in the php page..? Is this do-able..? any hints or code would help.
Thanks
Ashley
 
The best is to use database for that. If it's not possible, write not php code in details.txt file, but only values:

Code:
dad
dad
../pics/dad/
dad
viewer
dad@comports.net

admindad
admindad
dad
dad
../pics/dad/
admin
dad@comports.net

and so on...

Then in php file you don't write with switch, but

Code:
$file = file('details.txt');
foreach ($file as $key => $value) {
   $file[$key] = trim($value);
}
$i = 0;
$imax = count($file);
while ($i < $imax) {
   if ($gallery != $file[$i]) {
      while ($i < $imax && $file[$i] !== '') $i++;
      $i++;
   } else {
      $_pwd=$file[$i + 1];
      $gallery=$file[$i + 2];
      $gname=$file[$i + 3];
      $user=$file[$i + 4];
      $email=$file[$i + 5];
      break;
   }
}

I'm sure that there's some error in my script. Didn't check it practically. Problem of script - there may be problems if any field of information (_pwd, gname...) is empty. Must add some character in the beginning or something..
 
Back
Top