-2

I have seen another question with the same problem, but with just 1 answer, which didn't worked for me.

The question: PHP scandir - multiple directories

I'm using Joomla! to create my site (I create everything with templates, most that I don't understand creating an extension).

So, I created folder pbfData in main folder. In it I created folder users. But scandir does not detects any of those.

Errors (without if (is_dir))

Warning: scandir(/pbfData/) [function.scandir]: failed to open dir: No such file or directory in [...]/pbf/index.php on line 61

Warning: scandir() [function.scandir]: (errno 2): No such file or directory in [...]/pbf/index.php on line 61

Fatal error: Cannot access empty property in [...]/pbf/index.php on line 61

The code looks like this:

class users {

  var $users;

  function refUsers() {
    $dir = '/pbfData/';
    if (is_dir($dir)) {
      $this->$users = scandir($dir);
    } else {
      echo "<b>CRITICAL ERROR: </b>No access to users directory.";
    }
  }
[...] }

It outputs CRITICAL ERROR: No access to users directory.

(Note: adding is_dir to solve it was answer to the other question, but as I have written it doesn't work for me.)

Community
  • 1
  • 1
  • You're setting `$dir = '/pbfData/'` and then checking `is_dir($directory)` and then doing `scandir($dir)` so if `$directory` is a valid directory you're reading from `$dir` which may not be a valid directory. – apokryfos Jul 21 '16 at 15:58
  • Can you link the answer that didn't work and explain why? – Qaswed Jul 21 '16 at 16:08
  • @apokryfos I didnt saw, but even without the is_dir checking it didn't worked. –  Jul 21 '16 at 16:11
  • Well based on your code, I'm going to assume that "/pbfData/" isn't a directory. It's also an absolute path which looks odd. Perhaps you meant to do something like `__DIR__."/pbfData/"` ? – apokryfos Jul 21 '16 at 16:34

2 Answers2

1

You are checking if the folder /pdfData/ exists, and since the path starts with / what you are really checking is if you have a folder with the name pdfData inside your ROOT folder (C:\ or D:\ [etc] in windows, and / in linux/unix).

That means that if you run your script /var/www/html/pdf/index.php, event if you have the folder /var/www/html/pdf/pdfData - the function is_dir('/pdfData/') will return false, because you don't have that folder in your ROOT/

Folder structure
----------------
/pdfData/ <-- This is the folder that your code check (which doesn't exists)
/var/
/var/www/
/var/www/pdf/
/var/www/pdf/pdfData/ <-- This is the folder you want to check for

If you want to check the folder pdfData inside the current folder that you are running your code from, you should use $dir = 'pbfData/'; (without the starting slash)

Another options is to use the __DIR__ Magic constant:

$dir = __DIR__ . '/pbfData/';
Dekel
  • 60,707
  • 10
  • 101
  • 129
0

is_dir is not a solution to get directory path, it just checks if the file is a directory or not. To get the path you can use JPATH_SITE or JPATH_BASE etc as you are inside Joomla. Joomla already comes with built in constants. have a look here https://docs.joomla.org/Constants .

Your code will be

$dir = JPATH_BASE."/pbfData",
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
  • It works! Thanks! But there is still the Fatal error: Cannot access empty property in [...]/pbf/index.php on line 61. I'm not sure what does that means, but I'm going to search for it... –  Jul 22 '16 at 10:22
  • @Soaku If you can create a different question and post your code. I may be able to help you. You can check this links 1. http://stackoverflow.com/questions/2715610/where-is-the-fatal-error-cannot-access-empty-property-in-the-php-class-functio and 2. http://stackoverflow.com/questions/20437751/cannot-access-empty-property-in-php . This may help you. – Amit Ray Jul 22 '16 at 10:42
  • oh... Now I see what I have done wrong... I can't believe that I written `$this->$users` instead of `$this->users` –  Jul 22 '16 at 10:56
  • @Soaku Yes that was the mistake. minor but fatal. – Amit Ray Jul 22 '16 at 11:10