-1

I'm trying to turn the files in my 'objects' directory into an array, then use them to load the objects. But, for some reason, I continue to get this error

Warning: opendir(C:\xampp\htdocs/objects,C:\xampp\htdocs/objects): The system cannot find the file specified. (code: 2)

here is the code:

public function loadObjects(){
    $files = array();
    if ($handle = opendir(APP_PATH . 'objects')) 
    {
        while (false !== ($entry = readdir($handle))) 
        {
            if ($entry != "." && $entry != "..") 
            {
                $files[] = $entry;
            }
        }
    }

    closedir($handle);
    if(is_array($files) && count($files) > 0)
    {
        foreach($files as $value)
        {
            require_once(APP_PATH . 'objects/' . $value);
            $value = stristr($value, '.', true);
            self::$objects[$value] = new $object(self::$instance);
        }
    }
}
NoobEditor
  • 15,563
  • 19
  • 81
  • 112
user3656876
  • 1
  • 1
  • 2
  • Few things to check, does the directory definitely exist, and does it have perms to be accessed by apache? – scragar May 20 '14 at 13:50
  • What makes you think that the path "C:\xampp\htdocs/objects,C:\xampp\htdocs/objects" exists - is it even possible to create that path on windows? Look at the value of your `APP_PATH` constant. – AD7six May 20 '14 at 13:52
  • Might I suggest the PHP `glob()` function? – Adam Fowler May 20 '14 at 15:38

3 Answers3

1

I know this is an old question but for any future viewers I will post an anwser just in case. This type of error usually comes from a simple oversight. When developing most aplication the developer usualy uses a path like http://localhost/myAppHome or http://96.82.102.233/myAppHome(if on remote server)

In this perticular case the APP_PATH is probably defined somethig like that:

define('APP_PATH',$_SERVER['DOCUMENT_ROOT']);

This will be wrong in every case when the app is being developed outside of a domain name.

$_SERVER['DOCUMENT_ROOT'] will resolve to the root of domain which in this case will be

http://localhost or http://96.82.102.233

The main directory for localhost or the IP address is going to be the diretory root of the server itself => drive:/xampp/htdocs (for example)

Basically to avoid this issue you should always mind not to ask for 'DOCUMENT_ROOT' when developing without a domain pointing to you app.

If you dont require reqular deploys you can just add the missing folder to the definition like so :

define('APP_PATH',$_SERVER['DOCUMENT_ROOT'].'/myAppHome');

In case you deploy on reqular basis and you are afraid you will forget to rever this change before depoying you can always insert an IF when defing APP_PATH like:

if($_SERVER['SERVER_NAME']=='localhost'){
     define('APP_PATH', $_SERVER['DOCUMENT_ROOT'].'/myAppHome');
}else{
     define('APP_PATH', $_SERVER['DOCUMENT_ROOT']);
}
Daniel Doinov
  • 289
  • 3
  • 4
0

You are trying to open that directory with a "/".

Try to replace:

C:\xampp\htdocs/objects

to

C:\xampp\htdocs\objects


Please be sure APP_PATH variable is not null and correct values. There is no scandir function usage on your codes.

After that, i suggest you to use DirectoryIterator.

http://www.php.net/manual/en/class.directoryiterator.php

Complete example:

http://fabien.potencier.org/article/43/find-your-files

Canser Yanbakan
  • 3,780
  • 3
  • 39
  • 65
  • I've corrected the error message. The first message I received was from a different attempt using different code. Please take another look. – user3656876 May 20 '14 at 15:35
0

APP_HOST = DIR folder;
APP_PATH = APP_PATH + DIR folder;
Example = "C:/xampp/htdocs" + "/parent/child/index.php"

if ($_SERVER['SERVER_NAME'] == "localhost") {

  define('APP_HOST', pathinfo($_SERVER['PHP_SELF'], PATHINFO_DIRNAME));
  define('APP_PATH', $_SERVER['DOCUMENT_ROOT'] . APP_HOST);

} else {

  define('APP_PATH', $_SERVER['DOCUMENT_ROOT']);

}
antelove
  • 3,216
  • 26
  • 20