0

I was looking for some answers here but non of them worked for me what I have right now is

$directory = 'api\config\\';
$filecount = 0;
$files = glob($directory . '*.ini');

if ( $files !== false )
{
$filecount = count( $files );
echo $filecount;
}

The problem is $filecount is always 0

I'm using php 5.6 (required by the facility I'm working with). the directory I want to use (..\api\config).

any ideas why is this not working with me?

Muhannad
  • 467
  • 4
  • 28
  • 1
    Because your directory definition is wrong? `'api\config\\';` – Jay Blanchard May 15 '17 at 21:25
  • 1
    why there is multiple back slash? `$directory = 'api\config\\'` – Mahesh Singh Chouhan May 15 '17 at 21:26
  • Thanks for you answer.How can I define it appropriately I tried many formats nothing worked for me. Is there a secret with PHP. I'm not very familiar with PHP. – Muhannad May 15 '17 at 21:28
  • $directory = 'api/config'; – JYoThI May 15 '17 at 21:29
  • @JYoThI you need one more slash. – Jay Blanchard May 15 '17 at 21:30
  • 1
    @JayBlanchard The problem described in this question does not appear to be a duplicate of the question you have linked this to. – RToyo May 15 '17 at 21:31
  • Really @RobbieToyota. You don't think the duplicate describes how to count files in a directory? – Jay Blanchard May 15 '17 at 21:31
  • 1
    @JayBlanchard I viewed that question before it's not what I'm looking for I want to count a specific type of files *.ini. and one of the answers there pointed that but it didn't work for me that's why I asked. – Muhannad May 15 '17 at 21:32
  • may i know why need one more slash and where it need ? @JayBlanchard – JYoThI May 15 '17 at 21:32
  • `$directory = 'api/config/';` @JYoThI - note the last slash. – Jay Blanchard May 15 '17 at 21:34
  • 1
    @user3382285 - the last link in the dupes shows how to count JPEG's but could be used for INI files too. As a matter of fact, *your* code looks pretty much like *that* code except for the directory definition. – Jay Blanchard May 15 '17 at 21:35
  • 1
    @JayBlanchard It sounds like the OP already knows how to count files in a directory, and is instead asking why his code isn't working. Linking him to a question that doesn't address his problem doesn't seem helpful. – RToyo May 15 '17 at 21:37
  • @JayBlanchard agreed with Robbie you should not mark this answer as duplicate till that answer solves his problem! That OP clears already – Mahesh Singh Chouhan May 15 '17 at 21:38
  • 1
    Each duplicate demonstrates a working example of how to perform the operation. We've already pointed out why his code is failing. – Jay Blanchard May 15 '17 at 21:38
  • We'll just have to agree to disagree @MaheshSinghChouhan. If you don't think the duplicate is valid you can post on meta.stackoverflow.com and ask others to take a gander at the question. – Jay Blanchard May 15 '17 at 21:39
  • @JayBlanchard I don't mean to say that this is an original question. I was simply pointing out that "this is how you count files" is less helpful than a "why can't PHP find a directory", or "how to escape strings", or something like that. I'm relatively new to contributing to SO, so so I'm not too clear on exactly how duplicates should be linked. I didn't mean to derail this so far, and probably should have taken this to the chat. – RToyo May 15 '17 at 21:44
  • Generally duplicates are used when the question asked is very close to if not a complete rehash of another question on SO @RobbieToyota. It is intended to encourage folks to make sure they've done their research and proper troubleshooting *before* posting a question. We've all been guilty of it at some time or another. In addition, we generally expect that duplicates will have answers solving the majority of problems with an associated question. Dupes are by no means personal, nor are down-votes. – Jay Blanchard May 15 '17 at 21:49
  • Further @RobbieToyota, this seems like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) as evidenced by the OP's latest comment under your answer. – Jay Blanchard May 15 '17 at 21:53
  • @MaheshSinghChouhan read this comment and the associated link ^^^ – Jay Blanchard May 15 '17 at 21:53
  • @JayBlanchard Thanks for taking the time to clarify things for me! :) – RToyo May 15 '17 at 21:54
  • You bet @RobbieToyota! – Jay Blanchard May 15 '17 at 21:55

1 Answers1

1

You have forgotten to escape your first backslash. Use this:

$directory = 'api\\config\\';

One thing that will help for debugging in the future, is to try and output (echo) your $directory, to make sure the directory value is being passed to your functions correctly.

RToyo
  • 2,877
  • 1
  • 15
  • 22
  • 1
    What if....and I am just spit ballin' here....the OP is on Linux? – Jay Blanchard May 15 '17 at 21:32
  • when I used echo this got printed out (api\config\) does this sound correct? – Muhannad May 15 '17 at 21:36
  • @user3382285 I just re-read your question, and realized that you were looking for "..\api\config". You're missing the "..\\" in your directory path. And as Jay pointed out, can you confirm the OS that PHP is running on? You specifically used back slashes, and said you want to access a path with backslashes, so I assumed windows. – RToyo May 15 '17 at 21:39
  • Windows. I think I figured out the issue, but I still don't know how to fix it. When I put the complete path it works like this C:\\backoffice\\api\\config\\. people are installing the application to their machines and in different folders. I can't hard code the path.Any ideas my good sir why the (\\api\\config\\) alone doesn't work? – Muhannad May 15 '17 at 21:45
  • 1
    People are installing your PHP app on their machines @user3382285? Then you should have a setup script which defines certain directories. – Jay Blanchard May 15 '17 at 21:54
  • 1
    @user3382285 If an absolute path works, but a relative path doesn't, then perhaps your script isn't running from the location you think it is. You can use PHP's `getcwd()` to get your script's current working directory (output it), and then sort out your relative location from there. You can even use [chdir](http://php.net/manual/en/function.chdir.php) to test having your script navigate to your api\config folder, to make sure your relative location is correct. If this doesn't help you solve the problem on your own, please post back here with the results of your getcwd(). – RToyo May 15 '17 at 22:00
  • I used this line "define("CONFIG_DIRECTORY", dirname(dirname(__FILE__)) . "\\config\\");" and changed $directory = 'api\config\\'; to $directory = CONFIG_DIRECTORY;..it worked – Muhannad May 15 '17 at 22:00
  • just curious I used getcwd() and this is the directory that it prints C:\backoffice\api\scripts. the scripts is in there for sure. However, I want to count the files in C:\backoffice\api\config\ . Is there a smart why to make php find it? – Muhannad May 15 '17 at 22:06
  • 1
    @user3382285 Your repeated dirname() is similar to adding "..\". So rather than needing to do that, and because your script is already in folder within the api folder, use this path: `$directory = '..\\config\\';`. This first `..\\` will "bring your cwd" up one directory (to your api folder), and then go into your config folder. – RToyo May 15 '17 at 22:09