• 0

PHP - GetDirectories


Question

Hey Guys & Girls!

 

I'm fairly new to PHP, and I've been rollin' through the Codecademy courses, but it's not enough


I've been Googling around, looking for a good source of information, but I haven't found what I'm looking for. I guess you could say I'm wanting an "idiots guide to this issue".

I'm trying to show all the directories in x file i.e.

https://www.neowin.net/media

Any subfolder within that "media" folder would be listed in Alphabetical order. I feel bad for posting here with my level of knowledge. I just need a nudge in the right direction. I prefer to learn by doing, but this is a bit of a stretch for me. :(

 

Apologies if this isn't the right section!

Link to comment
Share on other sites

Recommended Posts

  • 0

Have you looked into using the PHP scandir function? http://php.net/manual/en/function.scandir.php

Kind of funny, you look like someone I know from School/Work.

As for the topic; I did look at scandir function, but frankly, I think the problem is the lack of knowledge for implementation. The good ol' copy n paste doesn't work for this sort've thing, and I'm having troubles with it.

Link to comment
Share on other sites

  • 0

You need to make sure you have correct permissions to read the folder. /tmp should be fine as it is 777 (usually), if that works chown the folder you want to list to the apache user (usually www-data) or just temporary chmod it to 777

Link to comment
Share on other sites

  • 0

As neoraptor said, you'll want to make sure you have the correct permissions. I do not have a job just yet and I haven't taken any classes at the local college for some time, I've been taking online classes for the last two years. Central Washington.

Have you checked out any tutorials on the topic?

Link to comment
Share on other sites

  • 0

You need to make sure you have correct permissions to read the folder. /tmp should be fine as it is 777 (usually), if that works chown the folder you want to list to the apache user (usually www-data) or just temporary chmod it to 777

Changed all my permissions for those folders to 777. I'll double check them. Does /tmp work as a universal?

 

As neoraptor said, you'll want to make sure you have the correct permissions. I do not have a job just yet and I haven't taken any classes at the local college for some time, I've been taking online classes for the last two years. Central Washington.

Have you checked out any tutorials on the topic?

Well, the company I work for, may be looking for another Day Shift Operations Associate, I'll talk to my boss and see whats up. If you can code PHP, you'd be more than qualifed, intelligence wise, for the job I do. It's pretty simple and easy.

<!doctype html>
<html>
<head>
	<title>FAIM - Archives</title>
</head>
<body>
<?php
$dir    = '/tmp';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);

print_r ($files1);
print_r ($files2);
?>
</body>
</html>

I think I'm doing something wrong, I'm lost lol!

Link to comment
Share on other sites

  • 0

If that's not working, you will want to double check your permissions. Is it giving any sort of error?

Link to comment
Share on other sites

  • 0

Try:

$dir =getcwd();

If this works then it will show the files in the current directory of you php file.

After that do:

echo getcwd();

This will show you what the current directory is, from here on out you can probably change this string to the directory you want.

Link to comment
Share on other sites

  • 0

Okay, one example. The file I'm using is CHMOD at 0644 which is "I can change, everyone else can read it." Using the code I link below, this returns everything in the directory.

 

http://unix.stackexchange.com/questions/26645/why-use-chmod-644-instead-of-chmod-u-rw-go-r

<?php

$dir = getcwd(); 
$items = scandir($dir);
echo '<pre>' , print_r($items) , '</pre>';
?>
Link to comment
Share on other sites

  • 0

If that's not working, you will want to double check your permissions. Is it giving any sort of error?

Blank page.

 

Try:

$dir =getcwd();

If this works then it will show the files in the current directory of you php file.

After that do:

echo getcwd();

This will show you what the current directory is, from here on out you can probably change this string to the directory you want.

Nope. Blank page.

 

 

http://www.faimindustry.net/media/anime

That's the link, ignore the files in the actual folders. I'm using this as a temporary host, for some things.

 

Okay, one example. The file I'm using is CHMOD at 0644 which is "I can change, everyone else can read it." Using the code I link below, this returns everything in the directory.

 

http://unix.stackexchange.com/questions/26645/why-use-chmod-644-instead-of-chmod-u-rw-go-r

<?php

$dir = getcwd(); 
$items = scandir($dir);
echo '<pre>' , print_r($items) , '</pre>';
?>

Sorry for double post;

 

The Folder

 

This is the error it returns.

Link to comment
Share on other sites

  • 0

Blank page.

 

Nope. Blank page.

 

 

http://www.faimindustry.net/media/anime

That's the link, ignore the files in the actual folders. I'm using this as a temporary host, for some things.

Sorry for double post;

 

The Folder

 

This is the error it returns.

Did you actually save the php file as .php?

Since the page is just showing the php code as plain text instead of executing it >.>

 

view-source:http://www.faimindustry.net/media/anime/

Link to comment
Share on other sites

  • 0

I hate my computer. That would really help. I just loaded Dreamweaver up (GASP I KNOW!) Let me try it then.

Error 500 - Internal Server Error.

Start step by step.

First try:

$dir = getcwd();
echo $dir;

Then:

$dir = getcwd();
$items = scandir($dir);
echo "<pre>".print_r($items)."</pre>";

I fail to comprehend why someone used commas in this line of code:

echo '<pre>' , print_r($items) , '</pre>';
Link to comment
Share on other sites

  • 0

 

Start step by step.

First try:

$dir = getcwd();
echo $dir;

Then:

$dir = getcwd();
$items = scandir($dir);
echo "<pre>".print_r($items)."</pre>";

I fail to comprehend why someone used commas in this line of code:

echo '<pre>' , print_r($items) , '</pre>';

None of that worked. I checked all my permissions, they're all 777, public_html/media/aanime are all 777.

I've gotta get some sleep, I work graveyard, I'll check back in the evening when I wake up. My buddy said I might not have "permissions" because it's a shared host. I use HostGator, but who knows. I don't have a .htaccess file in their either. Anyways, I'll check back in 7 - 9 hours.

Link to comment
Share on other sites

  • 0

 

Start step by step.

First try:

$dir = getcwd();
echo $dir;

Then:

$dir = getcwd();
$items = scandir($dir);
echo "<pre>".print_r($items)."</pre>";

I fail to comprehend why someone used commas in this line of code:

echo '<pre>' , print_r($items) , '</pre>';

No real reason, I was just demonstrating it. Do you mind my asking why you feel the need to make that statement? I normally use concatenation for strings. *shrugs*

Link to comment
Share on other sites

  • 0

 

Start step by step.

First try:

$dir = getcwd();
echo $dir;

Then:

$dir = getcwd();
$items = scandir($dir);
echo "<pre>".print_r($items)."</pre>";

I fail to comprehend why someone used commas in this line of code:

echo '<pre>' , print_r($items) , '</pre>';

Looks like it's working now. I had to change the permissions for the ENTIRE public_html folder to 755, and it worked a few hours later. Now, I need to clean it up a bit. Any way to remove some of the gunk?

 

 Array ( [0] => . [1] => .. [2] => Akame_Ga_Kill [3] => Akatsuki_No_Yona [4] => Amagi_Brilliant_Park [5] => Ao no Exorcist [6] => Argevollen [7] => index.php )

1


i.e. " Array (" and the ".." etc? The Index.php is kind've funny to see, lol.

Link to comment
Share on other sites

  • 0

No real reason, I was just demonstrating it. Do you mind my asking why you feel the need to make that statement? I normally use concatenation for strings. *shrugs*

My bad didn't knew echo supported multiple parameters in PHP.
Link to comment
Share on other sites

  • 0

Looks like it's working now. I had to change the permissions for the ENTIRE public_html folder to 755, and it worked a few hours later. Now, I need to clean it up a bit. Any way to remove some of the gunk?

Array ( [0] => . [1] => .. [2] => Akame_Ga_Kill [3] => Akatsuki_No_Yona [4] => Amagi_Brilliant_Park [5] => Ao no Exorcist [6] => Argevollen [7] => index.php )1
i.e. " Array (" and the ".." etc? The Index.php is kind've funny to see, lol.
$items is an array so you can walk trough the items with a for loop.

for($x=2;$x<count($items);$x++) {
echo $items[$x];
}
I start the for loop by index $x = 2 to skip the linux paths, keep in mind this only works when the folder has a parent folder.
Link to comment
Share on other sites

  • 0

To filter out the files and only echo the folders use is_dir().

Like this:

if(is_dir($items[$x])) {
echo $items[$x];
}
Link to comment
Share on other sites

  • 0

$items is an array so you can walk trough the items with a for loop.

for($x=2;$x<count($items);$x++) {
echo $items[$x];
}
I start the for loop by index $x = 2 to skip the linux paths, keep in mind this only works when the folder has a parent folder.

 

 

 

To filter out the files and only echo the folders use is_dir().

Like this:

if(is_dir($items[$x])) {
echo $items[$x];
}

Thanks, Seahorsepip! I just bought a new PHP / MySQL Book. I look forward to learning from it.

I'll test these pieces of code out, and then I'll start to shiny them up. I'll leave this "unsolved", for now. I'd like to find out why my folders didn't really chmod to 777, but 755 works. =/

Link to comment
Share on other sites

  • 0

Thanks, Seahorsepip! I just bought a new PHP / MySQL Book. I look forward to learning from it.

I'll test these pieces of code out, and then I'll start to shiny them up. I'll leave this "unsolved", for now. I'd like to find out why my folders didn't really chmod to 777, but 755 works. =/

chmod 777 might be disabled by file host since that is a very dangerous thing to do, everyone can write to that ftp folder when its 777.
Link to comment
Share on other sites

  • 0

chmod 777 might be disabled by file host since that is a very dangerous thing to do, everyone can write to that ftp folder when its 777.

I didn't think of it like that, sounds good. I'll be home in 2 hours, and I'll check that code out there. This has been fun so far!

  • Like 1
Link to comment
Share on other sites

  • 0

My bad didn't knew echo supported multiple parameters in PHP.

Echo treats using "," as adding separate arguments into echo. You can use it if you want from what I understand of my "research" of it, but if I understood correctly there is not any real gain from it. On the other hand print will break if you use "," so it may be less confusing to just use "." for that. Sorry though, I probably should have explained that when I posted that, I haven't really had anyone proof much of my code and I've always been cleaning up someone else's code

Link to comment
Share on other sites

  • 0

Echo treats using "," as adding separate arguments into echo. You can use it if you want from what I understand of my "research" of it, but if I understood correctly there is not any real gain from it. On the other hand print will break if you use "," so it may be less confusing to just use "." for that. Sorry though, I probably should have explained that when I posted that, I haven't really had anyone proof much of my code and I've always been cleaning up someone else's code

Just didn't knew it was possible with php because I've never seen it in any code before :p

Link to comment
Share on other sites

  • 0

I normally use glob() for reading directory contents

Yeah that's also a nice method of reading contents, I've never actually used it before since I only do simple file based tasks in php but for pattern based file matching and recursive directories it's definitely a better choice.

Link to comment
Share on other sites

This topic is now closed to further replies.