Parse error: syntax error, unexpected 'filename' (T_STRING) in C:\xampp\htdocs\php\dir.php on line 9 [closed]

StackOverflow https://stackoverflow.com/questions/23475546

  •  15-07-2023
  •  | 
  •  

Question

**php program for reading filenames and filetypes from a dir.**

it shows the syntax error : Parse error: syntax error, unexpected 'filename' (T_STRING) in C:\xampp\htdocs\php\dir.php on line 9

<?php
$dir="C:\xampp\htdocs\php\"; 
if(is_dir($dir)) 
 {
 if($dirref=opendir($dir))
   {
     while(($file=readdir($dirref))!==false)
      {
      echo "filename : $file : filetype: ".filetype($dir.$file)."\n";
      }
   closedir($dirref);
   }
 }
?>
Was it helpful?

Solution

It appears you've escaped the closing quotes on your $dir= statement. I don't use php in a Windows environment, so I don't know if you can convert all your \ to /, but if not, you'll want to escape all the \:

$dir="C:\\xampp\\htdocs\\php\\"; 

Or use single quotes, which disables escape sequences:

$dir='C:\xampp\htdocs\php\'; 

Parse errors often indicate that there is a syntax (or string in this case) error in your code, BEFORE the error actually reported by the parser.

OTHER TIPS

The error is on your first line: $dir="C:\xampp\htdocs\php\";

The backslash escapes the quotes.

Escape the backslashes in your code to make it work, like so:

$dir="C:\\xampp\\htdocs\\php\\"; 

$dir="C:\xampp\htdocs\php\";

Just remove the last '\' and make it

$dir="C:\xampp\htdocs\php";

It will Work..!

Change this:

$dir="C:\xampp\htdocs\php\";

to

$dir="C:\\xampp\\htdocs\\php\\";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top