Page 1 of 1

Really confused by code error

Posted: Fri Feb 03, 2017 5:27 pm
by Georgezx9
I have the following code which is essentially a cut and paste from the PHP website, and I cannot get it to work.

I get the following error:

Parse error: syntax error, unexpected ' ' (T_STRING) in G:\wamp\www\test100.php on line 6, I know thet for theswe error the fault usually lies before the line quoted, but I cannot see what.

Line 6 in Notepad++ is: if (is_dir($dir)) {

Any help greatly appreciated.

GY

Code: Select all

<?php
//$dir = "/etc/php5/";
$dir = "images/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>

Re: Really confused by code error

Posted: Fri Feb 03, 2017 10:09 pm
by requinix
You may have copied a non-breaking space, and PHP doesn't care for those. Delete all the spaces from the line and add them back in manually.

Re: Really confused by code error

Posted: Sat Feb 04, 2017 7:25 pm
by Vegan
i suggest using notepad configured for using terminal font which will sanitize any HTML fast

Re: Really confused by code error

Posted: Sun Feb 12, 2017 5:55 pm
by Pazuzu156
Not too sure what's up. I copied the code and it worked just fine for me. However, I usually tend to do the same flow a tad different.

This is how I reformatted the code, it still does exactly what yours does:

Code: Select all

<?php

$dir = 'images/';

if (is_dir($dir)) {
    $handle = opendir($dir);
    $ignore = ['.', '..'];

    while (($file = readdir($handle)) !== false) {
        if (!in_array($file, $ignore)) {
            echo 'filename: '.$file.' : filetype: '.filetype($dir.$file).'<br>';
        }
    }

    closedir($handle);
}
An example showing this working can be found here: https://testsites.kalebklein.com/phpdn/1/