Thursday, July 9, 2009

Php error i get: PHP Parse error: syntax error, unexpected T_STRING in C:\Inetpub\wwwroot\index.php on line 12

Basically my php script reads:


%26lt;?php


$path = "C:\Inetpub\wwwroot";


$dir_handle = @opendir($path) or die("Unable to open $path");


while ($file = readdir($dir_handle)) {


if($file == ".html" || $file == ".." || $file == "index.php" )


continue;


echo "%26lt;a href=\"$file\"%26gt;$file%26lt;/a%26gt;
";


}


closedir($dir_handle);


if (count($files)) {


foreach ($files as $file){


list($name,$ext)=split($file,"...print... href=\.$file."\"%26gt;".$name.");


}


}


?%26gt;








Everything works upto:closedir($dir_handle);





When i include the rest, which should be readng the first couple of lines of the documents that are listed, then it doesn't work, and returns this t_string error, have checked the double quotes and also the brackets match up.





I am using iis web server with php on my windows xp.





really confused, though i am new to php, thanks,





regards





Tovia

Php error i get: PHP Parse error: syntax error, unexpected T_STRING in C:\Inetpub\wwwroot\index.php on line 12
Why did you do split($file,"... href=\.$file."\"%26gt;".$name."); ??





And you didn't create $files before using it.





I don't know exactly what do you want to do, but I just try to rewrite the script (that lists all directories and files in the "root" dir that you define with $path), like that:





%26lt;?php


$path = "./";


$dirs = array();


$files = array();


$opendir = @opendir($path) or die("Unable to open $path");


while($item = readdir($opendir))


{


if(!in_array($item, array(".", "..", ".html", "index.php")))


{


if(is_dir($path.$item)) $dirs[] = $item;


else $files[] = $item;


echo "%26lt;a href=\"".$path.$item."\"%26gt;".$item."%26lt;/a%26gt;
";


}


}


closedir($opendir);


echo "
";


$filename_splitted = array();


if(count($files))


{


$i = 0;


foreach ($files as $file)


{


if(strpos($file, "."))


{


/* I don't use split() or preg_match() here


because we can have a file like "abc.xyz.php",


that contains 2 "." or more! */


$filename_splitted[$i]['name'] = substr($file, 0, strrpos($file, "."));


$filename_splitted[$i]['ext'] = substr($file, strrpos($file, ".")+1, strlen($file));


}


else


{


$filename_splitted[$i]['name'] = $file;


$filename_splitted[$i]['ext'] = "";


}


$i++;


}


}





echo "%26lt;pre%26gt;";


print_r($dirs);


print_r($filename_splitted);


echo "%26lt;/pre%26gt;";


?%26gt;





(For indented code, you can see here: http://dotructhanh.info/temp/listdir.200... )
Reply:If you want to get some code inside of each HTML file, you can use fgets() or fread().





If you want to list *.html files, try to use preg_match() (for regular expressions).





Or if you don't really understand what I'm telling you, please write another question more explicit! Report It

Reply:It looks like you have a quote problem in your split function.


I'm not sure what you're trying to split, but the following is the proper amount of quotes:





split($file,"... href=\.$file."%26gt;".$name.");





or something like this:





list($name,$ext)=split($file, "href= \"$file\"%26gt; $name" );





or maybe:





split($file,"...href= \"$file\"%26gt; $name");





basically, you had one too many quotes.
Reply:Possible that you closed the directory too early? Try moving closdir() further down past the foreach loop.

apricot

No comments:

Post a Comment