2

I scan a directory with PHP which includes only folders:

$folders = scandir('gallery');

Now i want to check if a string in javascript a folder in this directory.

if(theString == allTheFolders){
     alert('yay');
}

Now $folders is an array with strings in it. To get all the strings i use a foreach loop and ignore the '.' & '..' directory's. But how can i get all these folders in the if loop? Hope you understand my question!

6
  • you can't compare a string with a PHP-Array! you can only compare the string with one folder! Commented Dec 6, 2012 at 15:57
  • @Karen: Uhm, yes i know. so this is my Question! 'allTheFolders' stays for compare the string with all found folders... Commented Dec 6, 2012 at 15:58
  • JS is visible to the client, so I don't know if security means a great del to you, but displaying your complete folder structure to possible attackers doesn't seem like a good idea to me... Commented Dec 6, 2012 at 15:59
  • 1
    @11684: gallery's folder structure might not be security-essential ;) Commented Dec 6, 2012 at 16:01
  • @11684: I create with uploads new folders. So i don't want for every new album edit my javascript. So i want to scan only the gallery folder to get all albums :) Commented Dec 6, 2012 at 16:03

1 Answer 1

4

Echo out your array as JSON, right into your JavaScript.

echo 'var folders = ', json_encode($folders);

Then you can loop through or do whatever you need directly in JavaScript.


Edit: Now that you have posted your actual question... Do this in your JavaScript:

var wantedFolder = 'something';
var wantedFolderFound = false;
for (folderIndex in folders) {
    if (folders[folderIndex] === wantedFolder) {
        wantedFolderFound = true;
    }
}
if (wantedFolderFound) {
    alert('Folder found!');
} else {
    alert('Folder not found.');
}

As an alternative, I would probably use Array.indexOf(). It isn't available in all browsers, but that problem is easily remedied. See the documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf

Sign up to request clarification or add additional context in comments.

5 Comments

Brilliant, I had the json_encode, but didn't know how to get it to the JS! +1
Okay. I think, var folders is now a array... Uhm. how can i now insert all the folders in my if loop? :S
@dTDesign, Your question makes no sense. Are you trying to loop through all folders? developer.mozilla.org/en-US/docs/JavaScript/Reference/…
When i see it correct, i get now a array of all folders. Now my target is to check, if my string is a album. So i must compare it with all the results in the folders-array. But i don't know how i should do that...
Oh ok, i think it isn't a array. I try it with google... +1 for your help, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.