Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.