Jump to content

is_dir not working in php 5.2.13


Chappers

Recommended Posts

Hi,

 

I've read of is_dir problems in older versions of php, so checked and server is using 5.2.13.

 

This simple bit of code:

 

<?php

$contents =  scandir('/home/*******/public_html/images/');

foreach ($contents as $content) {
if (is_dir ($content)) {
echo "$content is a directory<br>";
}

else {
echo "$content is NOT a directory<br>";
}}

?>

 

outputs:

 

. is a directory
.. is a directory
folder1 is NOT a directory
folder2 is NOT a directory
folderthree is NOT a directory

 

Those are the three folders I created for test purposes. Is_file works as anticipated, and I know I can use it and other workarounds. But why wouldn't is_dir be working? Have I done something wrong?

Link to comment
https://forums.phpfreaks.com/topic/241462-is_dir-not-working-in-php-5213/
Share on other sites

LOL, because each folder has a . and .. entry, you will get a TRUE from the is_dir(), no matter where your script is executed at.

 

The only way your script would give you TRUE values from is_dir() for specific folders within the images folder, is if the script is executed from inside the images folder.

 

You would need to build either a relative or absolute path in the if(){} statement -

 

if (is_dir ($path/$content)) {

How did you create these directories exactly?

 

Using flashfxp, I FTP'ed to the website and right-clicked and chose make folder.

 

@PFMaBiSmAd: Two problems with your post; the LOL which is patronising, but since you obviously know so much, I'm sure it's acceptable to take the p*ss out of those who don't. Second problem; if you read my post including the code output, you'd see is_dir ISN'T returning TRUE for everything it finds like you claim it must do.

You need to give is_dir the full path to the folder. You cant just give is_dir the name of the folder. Change your code to

 

$search_path = '/home/*******/public_html/images/';
$contents =  scandir($search_path);

foreach ($contents as $content) {
if (is_dir ("$search_path/$content")) {
echo "$search_path/$content is a directory<br>";
}

else {
echo "$search_path/$content is NOT a directory<br>";
}}

 

Here's the first statement written out with all the qualifications and conditions, since it was apparently misunderstood -

 

because each folder (except the disk root) has a . and .. entry, you will get a TRUE from the is_dir() (for the . and .. entry, what this sentence is referring to), no matter where your script is executed at.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.