Really confused by code error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Georgezx9
Forum Newbie
Posts: 6
Joined: Sat Dec 10, 2016 3:29 am

Really confused by code error

Post 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);
    }
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Really confused by code error

Post 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.
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: Really confused by code error

Post by Vegan »

i suggest using notepad configured for using terminal font which will sanitize any HTML fast
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Really confused by code error

Post 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/
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply