Jump to content

opendir and Loops


Guest MrLeN
Go to solution Solved by Ch0cu3r,

Recommended Posts

I have 2 paces of code that work by themselves. But when I try to put 1 inside the other, it wont work. Basically, I want to bit the bottom code inside the top code where I have a comment "<!---SECOND CODE IN HERE--->":

 

I have been working on this all day and now it's  5:17am.

 

As far as I can work out, I think it has something to do with opendir twice.. because the codes work by themselves (I tested them).. but one wont work inside the other. I am completely out of ideas

I know you can put a loop n a loop because I tested it. I think it's the opendirs.

 

I get the error:

 

Warning: readdir(): 372 is not a valid Directory resource

 

1st code:

$dir = '/home/tool/public_html/referrals_bridge/';
 
if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && $entry != ".php" && $entry != ".txt" && $entry != "error_log") {
            //echo "$entry\n";
$affiliate_id = file_get_contents($dir . $entry);
$user_id = $entry;
//echo "Referral ID:" . $affiliate_id . "<br />";
$user_id = str_replace('.txt', '', $user_id);
echo "User ID:" . $user_id . "<br />";
//$user = $entry;
//$user_role = file_get_contents('/home/tool/public_html/referrals/'.$affiliate_id.'/role.txt');
//exit;
//Get user information
           $user_info = get_userdata($user_id);
           $paypal_email = $user_info->paypal_email;
           $contact_email = $user_info->user_email;
           $user_role = implode(', ', $user_info->roles);
           echo $user_role . "<br />";
           $user_role  =preg_replace('/^([^,]*).*$/', '$1', $user_role);
 
 
 
<!---SECOND CODE IN HERE--->
 
           echo '<table summary="" cellpadding="0" cellspacing="0" border="1" style="font-size: 12px; font-family: arial"><tr>';
           echo '<td>ID</td><td>Role</td><td>Aff ID</td><td>Refs</td><td>Free</td><td>VIP</td><td>Commission</td><td>email</td></tr><tr>'; 
           echo '<td nowrap>'.$user_id.'</td>';
           echo '<td nowrap style="width: 200px;">'.$user_role.'</td>';
           echo '<td nowrap>'.$affiliate_id.'</td>';
           echo '<td nowrap>'.$totalMembers.'</td>';
           echo '<td nowrap>'.$UnSubscribedMembers.'</td>';
           echo '<td nowrap>'.$SubscribedMembers.'</td>';
           echo '<td nowrap><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$paypal_email.'&item_name=toolHost+Commission&item_number='.$user_id.'&currency_code=USD&lc=CA&charset=UTF%2d8&amount='.$referral_commissions.'" target="_blank">'.$referral_commissions.'</a></td>';
 
           echo '<td nowrap><a href="mailto:'.$contact_email.'&subject=Re: toolHost Commissions">contact</a></td>'; 
           echo '</tr></table>'; 
 
        }
    }
    closedir($handle);
}

2nd code:

    // initialize counters to zero
    $SubscribedMembers = 0;
    $UnSubscribedMembers = 0;
 
    //$current_user = wp_get_current_user();
    $dir = '/home/tool/public_html/referrals/' . basename($affiliate_id);
    if ($handle2 = opendir($dir)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != ".." && $entry != "role.txt"&& $entry != "promos") {
                //echo "$entry\n";
                $path = "$dir/$entry";
                $result = file_get_contents($path);
 
                switch(trim($result))
                {
                    // increment subscribed couter, if the contents of the file is "subscribed"
                    case 's2member_level2':
                        $SubscribedMembers++;
                    break;
 
                    //  increment unsubscribed couter, if the contents of the file is "unsubscribed"
                    case 's2member_level1':
                    default:
                        $UnSubscribedMembers++;
                    break;
                } 
            }
        }
        closedir($handle2);
    }
 
    // calculate total members
    $totalMembers = $SubscribedMembers + $UnSubscribedMembers;
 
    // output results
    $referral_commissions = ($SubscribedMembers * '7.5');
 
 
//clean up long names:
if ($user_role == "subscriber") {
  $user_role = "Free";
}
if ($user_role == "s2member_level1") {
  $user_role = "Entrepreneur";
}
if ($user_role == "s2member_level2") {
  $user_role = "VIP";
}
Edited by MrLeN
Link to comment
Share on other sites

That is the code in its entirety.

 

Here it is together:

 

My putting it into two separate files that work was my attempt at making it easier :)

 

 

$dir = '/home/tool/public_html/referrals_bridge/';
 
if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && $entry != ".php" && $entry != ".txt" && $entry != "error_log") {
            //echo "$entry\n";
$affiliate_id = file_get_contents($dir . $entry);
$user_id = $entry;
//echo "Referral ID:" . $affiliate_id . "<br />";
$user_id = str_replace('.txt', '', $user_id);
echo "User ID:" . $user_id . "<br />";
//$user = $entry;
//$user_role = file_get_contents('/home/tool/public_html/referrals/'.$affiliate_id.'/role.txt');
//exit;
//Get user information
           $user_info = get_userdata($user_id);
           $paypal_email = $user_info->paypal_email;
           $contact_email = $user_info->user_email;
           $user_role = implode(', ', $user_info->roles);
           echo $user_role . "<br />";
           $user_role  =preg_replace('/^([^,]*).*$/', '$1', $user_role);
 
 
 
    // initialize counters to zero
    $SubscribedMembers = 0;
    $UnSubscribedMembers = 0;
 
    //$current_user = wp_get_current_user();
    $dir = '/home/tool/public_html/referrals/' . basename($affiliate_id);
    if ($handle2 = opendir($dir)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != ".." && $entry != "role.txt"&& $entry != "promos") {
                //echo "$entry\n";
                $path = "$dir/$entry";
                $result = file_get_contents($path);
 
                switch(trim($result))
                {
                    // increment subscribed couter, if the contents of the file is "subscribed"
                    case 's2member_level2':
                        $SubscribedMembers++;
                    break;
 
                    //  increment unsubscribed couter, if the contents of the file is "unsubscribed"
                    case 's2member_level1':
                    default:
                        $UnSubscribedMembers++;
                    break;
                } 
            }
        }
        closedir($handle2);
    }
 
    // calculate total members
    $totalMembers = $SubscribedMembers + $UnSubscribedMembers;
 
    // output results
    $referral_commissions = ($SubscribedMembers * '7.5');
 
 
//clean up long names:
if ($user_role == "subscriber") {
  $user_role = "Free";
}
if ($user_role == "s2member_level1") {
  $user_role = "Entrepreneur";
}
if ($user_role == "s2member_level2") {
  $user_role = "VIP";
}
 
           echo '<table summary="" cellpadding="0" cellspacing="0" border="1" style="font-size: 12px; font-family: arial"><tr>';
           echo '<td>ID</td><td>Role</td><td>Aff ID</td><td>Refs</td><td>Free</td><td>VIP</td><td>Commission</td><td>email</td></tr><tr>';
           echo '<td nowrap>'.$user_id.'</td>';
           echo '<td nowrap style="width: 200px;">'.$user_role.'</td>';
           echo '<td nowrap>'.$affiliate_id.'</td>';
           echo '<td nowrap>'.$totalMembers.'</td>';
           echo '<td nowrap>'.$UnSubscribedMembers.'</td>';
           echo '<td nowrap>'.$SubscribedMembers.'</td>';
           echo '<td nowrap><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$paypal_email.'&item_name=toolHost+Commission&item_number='.$user_id.'&currency_code=USD&lc=CA&charset=UTF%2d8&amount='.$referral_commissions.'" target="_blank">'.$referral_commissions.'</a></td>';
 
           echo '<td nowrap><a href="mailto:'.$contact_email.'&subject=Re: toolHost Commissions">contact</a></td>';
           echo '</tr></table>';
 
        }
    }
    closedir($handle);
}
Edited by MrLeN
Link to comment
Share on other sites

if ($handle2 = opendir($dir))

{

while (false !== ($entry = readdir($handle)))

{

[\php]

 

You opened $handle2 and tried to read from $handle.

 

PS - you can write it like this:

 

while ($entry = readdir($handle2))

 

instead of the more complicated way you did it.

Edited by ginerjm
Link to comment
Share on other sites

if ($handle2 = opendir($dir))

{

while (false !== ($entry = readdir($handle)))

{

[\php]

 

You opened $handle2 and tried to read from $handle.

 

PS - you can write it like this:

 

while ($entry = readdir($handle2))

 

instead of the more complicated way you did it.

Oh I forgot I changed it to handle2 - they were both just 'handle'.. and I figured maybe there was some kind of conflict.

 

I'll revise that again and see if it's still giving me errors.

 

*because there are 2 open dirs one inside the other, I suspect that might be causing the problem. That's why I started trying to change the name of the handles.

 

I worked on this all day yesterday... I just work up. back at it again now.

 

I'll start by changing everything back to handle:

$dir = '/home/tool/public_html/referrals_bridge/';
 
if ($handle = opendir($dir)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && $entry != ".php" && $entry != ".txt" && $entry != "error_log") {
            //echo "$entry\n";
$affiliate_id = file_get_contents($dir . $entry);
$user_id = $entry;
//echo "Referral ID:" . $affiliate_id . "<br />";
$user_id = str_replace('.txt', '', $user_id);
echo "User ID:" . $user_id . "<br />";
//$user = $entry;
//$user_role = file_get_contents('/home/tool/public_html/referrals/'.$affiliate_id.'/role.txt');
//exit;
//Get user information
           $user_info = get_userdata($user_id);
           $paypal_email = $user_info->paypal_email;
           $contact_email = $user_info->user_email;
           $user_role = implode(', ', $user_info->roles);
           echo $user_role . "<br />";
           $user_role  =preg_replace('/^([^,]*).*$/', '$1', $user_role);
 
 
 
    // initialize counters to zero
    $SubscribedMembers = 0;
    $UnSubscribedMembers = 0;
 
    //$current_user = wp_get_current_user();
    $dir = '/home/tool/public_html/referrals/' . basename($affiliate_id);
    if ($handle = opendir($dir)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != ".." && $entry != "role.txt"&& $entry != "promos") {
                //echo "$entry\n";
                $path = "$dir/$entry";
                $result = file_get_contents($path);
 
                switch(trim($result))
                {
                    // increment subscribed couter, if the contents of the file is "subscribed"
                    case 's2member_level2':
                        $SubscribedMembers++;
                    break;
 
                    //  increment unsubscribed couter, if the contents of the file is "unsubscribed"
                    case 's2member_level1':
                    default:
                        $UnSubscribedMembers++;
                    break;
                } 
            }
        }
        closedir($handle);
    }
 
    // calculate total members
    $totalMembers = $SubscribedMembers + $UnSubscribedMembers;
 
    // output results
    $referral_commissions = ($SubscribedMembers * '7.5');
 
 
//clean up long names:
if ($user_role == "subscriber") {
  $user_role = "Free";
}
if ($user_role == "s2member_level1") {
  $user_role = "Entrepreneur";
}
if ($user_role == "s2member_level2") {
  $user_role = "VIP";
}
 
           echo '<table summary="" cellpadding="0" cellspacing="0" border="1" style="font-size: 12px; font-family: arial"><tr>';
           echo '<td>ID</td><td>Role</td><td>Aff ID</td><td>Refs</td><td>Free</td><td>VIP</td><td>Commission</td><td>email</td></tr><tr>'; 
           echo '<td nowrap>'.$user_id.'</td>';
           echo '<td nowrap style="width: 200px;">'.$user_role.'</td>';
           echo '<td nowrap>'.$affiliate_id.'</td>';
           echo '<td nowrap>'.$totalMembers.'</td>';
           echo '<td nowrap>'.$UnSubscribedMembers.'</td>';
           echo '<td nowrap>'.$SubscribedMembers.'</td>';
           echo '<td nowrap><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$paypal_email.'&item_name=toolHost+Commission&item_number='.$user_id.'&currency_code=USD&lc=CA&charset=UTF%2d8&amount='.$referral_commissions.'" target="_blank">'.$referral_commissions.'</a></td>';
 
           echo '<td nowrap><a href="mailto:'.$contact_email.'&subject=Re: toolHost Commissions">contact</a></td>'; 
           echo '</tr></table>'; 
 
        }
    }
    closedir($handle);
}
 

i got this  message:

 

Warning: readdir(): 518 is not a valid Directory resource in/home/tool/public_html/inc/member-commissions.php on line 5

Warning: closedir(): 518 is not a valid Directory resource in/home/tool/public_html/inc/member-commissions.php on line 90

Edited by MrLeN
Link to comment
Share on other sites

  • Solution

No you need to have different handles. The second $handle (for reading the referrals directory) will be overwriting the first $handle (for reading the referrals_bridge directory).

 

@ginerjm

 

 

 

PS - you can write it like this:

while ($entry = readdir($handle2))

instead of the more complicated way you did it.

The PHP manual recommends you write it that way.

Edited by Ch0cu3r
Link to comment
Share on other sites

The language has a unique way of testing the readdir function?

It has nothing to do with the readdir function, it has to do with PHP's loose type.

 

Did you skip this part in the manual?

Warning

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

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.