Jump to content

Having trouble with opendir


Argonust

Recommended Posts

Hi

I am have trouble with making this bit of code work

if(!isset($_GET['m'])) {
  die();
 }
$model = ($_GET['m']);
 
$images = "thumbs/$model/";
$big    = "gallery/$model/";

if($handle=opendir($images)) {
	while(false!==($file=readdir($handle))) {
	  if($file !=="."&&$file !=".."&&$file !=rtrim($big,"/")) {
	    $files[]=$file;
       }
    }
  closedir($handle);
 }

When I run this code nothing happens. I get this error message

PHP Warning:  opendir(thumbs/Honda/): failed to open dir: Permission denied in /var/www/html/Cars/index.php on line 56, referer: http://localhost/Cars

After some playing around I found that the line

$images = "thumbs/$model/";

is the problem. If I change it to

$images = "thumbs";

Then it works, my test was done using  "print_r($file);" This shows the array in the browser.

I have changed all the permissions in the thumbs directory and its sub-directories to 776.

This is also the case when using scandir()

 

Any help would be appreciated

Thank you

Link to comment
Share on other sites

You may want to check these things:

 

1. If directory thumbs/Honda/ really exists and can be opened by PHP script. I would start with this code:

if (!is_dir("thumbs/$model/"))
    {
    echo "dir thumbs/$model/ doesn't exist!";
    }

Most likely you won't get an error here (otherwise your function would display an error not related to Permission denied), but still worth to check.

 

2. Check directory ownership. I don't know how you create sub-directories inside /thumbs, but depending on server configuration and the way you create directories, you might not be able to enter them sometimes because of wrong ownership. For example, directory's owner might be set as root, while script runs as another user. This way, permission error will occur.

 

3. Try 755 permissions. 

Link to comment
Share on other sites

Ok - had to format your code a bit to get a handle on it.

if(!isset($_GET['m'])) 
{
	echo "Argument 'm' is not present.  Try again";
	exit();
}
$model = $_GET['m'];
 
$bad_m = false;
$images = 'thumbs/' . $model . '/';
$big    = 'gallery/' . $model . '/';
if (!is_dir($images)
{
	echo "Invalid 'images' folder - '$images' <br>";
	$bad_m = true;
}
if (!is_dir($big)
{
	echo "Invalid 'big' folder - '$big' <br>";
	$bad_m = true;
}
if ($bad_m)
{
	echo 'Please try again';
	exit();
}
//    folders are valid.
echo "Images folder is $images<br>";
echo "Gallery folder is $big<br>";
if($handle = opendir($images)) 
{
	echo "Opened folder: $images<br>";
	$file_cnt = 0;
	while($file = readdir($handle))) 
	{
	  if($file !== "." && 
			$file != ".." && 
			$file != rtrim($big,"/")) 
		{
			$files[]=$file;
			$file_cnt++;
		}
    }
	closedir($handle);
	echo "Found $file_cnt files in folder: $images<br>";
}
Added some echo's to check the progress of the script as it runs. Also to show the values being used.

I see nothing bad here but do wonder about the check for the $big folder while processing the images folder. Looking at the specific path you are using the images path is relative to the folder wherein this script is executing. AND - the big path is also relative to the current folder. Therefore - your loop should never run into the big folder while processing the images folder. Why do you check for it then?

  • Like 1
Link to comment
Share on other sites

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.