[SOLVED] Readdir question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

[SOLVED] Readdir question

Post by Sphen001 »

Hi,

Right now, I'm trying to find a way to scan a certain directory of files that match some conditions (I know how to do this part), and append their contents to an array.

The files just hold language strings. The format is this:

Code: Select all

$lang += array(
'Some thing' => 'Some value',
'Another thing' => 'Another value'
);
*Note: If I just include the file and use the array, ex

Code: Select all

$lang['Hi'] // Outputs hi
so I know that's not the problem.

The code I am using for getting the content is:

Code: Select all

function getLangFile($dir)
     {
          if (is_dir($dir))
          {
               if ($handle = opendir($dir))
               {
                    while (FALSE !== ($file = readdir($handle)))
                    {
	          if ($file !== "." && $file != "..")
	          {
	              // Up to here works fine
                              if (ereg('lang', $file))
                              {
	                   require($dir . $file);
	                   $lang[] += $lang;
	               }
                         }
	    }
			
	    closedir($handle);
                    return $lang;
               }
          }
     }
For some reason, it won't add the contents of the lang file to the master lang array.

If anyone could help me with this, It would be greatly appreciated.

Thanks,

Sphen001
Last edited by Sphen001 on Tue Jul 12, 2005 9:16 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if your included file indeed does contain $lang += .. then you do not need to do any more with $lang, provided $lang was already an array.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Your nesting takes some deciphering too :wink:
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Hi,

Sorry, I didn't quite say everything. What I want to do is run the function I have and it will automatically open all the files with a filename of lang_* and add their contents to the master $lang array. I'm doing this because I'm developing a system where people can add their own modules, and I don't want people to have to add hundreds of new language strings everytime they install a new module.

Sorry about the confusion.

Sphen001
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

d11wtq wrote:Your nesting takes some deciphering too :wink:
That's the way it copied and pasted. The code in my file is very neat.

EDIT:

If you want to see the file itself.

http://phpscms.sourceforge.net/functions.txt
Last edited by Sphen001 on Sat Mar 12, 2005 9:25 am, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$contents = file_get_contents($dir.$file);
$lang[] = $contents;
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Hmm...

Whenever I tried that, it didn't work. I'll try it again though.

Thanks,

Sphen001
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

To debug

Code: Select all

if (is_file($dir.$file)) {
    $contents = file_get_contents($dir.$file);
    $lang[] = $contents;
} else {
   echo $dir.$file .' is not a valid file';
}
Last edited by Chris Corbyn on Sat Mar 12, 2005 9:29 am, edited 1 time in total.
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Okay, cool.

I'll try that and let you know if it works.

Thanks again,

Sphen001
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<?php
//------------------------------------------------------------------------------
// File         : functions.php
// Description  : Contains functions common to every page.
// Author       : Sphen001 (Nick Ross)
// Date Started : Mar 5, 05
// Support      : http://phpscms.sourceforge.net
// Contact      : Sphen001 - at - users - dot - sourceforge - dot - net
// Copyright    : 2005 phpSCMS Group
// $Id: functions.php,v 1.1 2005/03/06 02:28:07 sphen001 Exp $
//------------------------------------------------------------------------------

// Grabs the contents of every lang file in the directory

function getLangFile($dir)
{
	if (is_dir($dir))
	{
		if ($handle = opendir($dir))
		{
			while (FALSE !== ($file = readdir($handle)))
			{
				if ($file !== &quote;.&quote; && $file != &quote;..&quote;)
				{
					if (ereg('lang', $file))
					{
						require($dir . $file);
						$langї] += $alang;
					}
				}
			}
			
			closedir($handle);
		}
 	}
}

// PHP header redirection

function redirect($url)
{
	header (&quote;Location: &quote; . $url);
}

// META redirection

function metaRedirect($url, $time)
{
	echo '<meta http-equiv=&quote;refresh&quote; content=&quote;' . $time . ';url=' . $url . '&quote;>';
}
?>

seems to copy and paste in just fine for me. :P
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Maybe it just doesn't like me :wink:
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Well, I tried the fix above and it didn't work.

Here is the new function code:

Code: Select all

function getLangFile($dir)
{
	if (is_dir($dir))
	{
		if ($handle = opendir($dir))
		{
			while (FALSE !== ($file = readdir($handle)))
			{
				if ($file !== "." && $file != "..")
				{
					if (ereg('lang', $file))
					{
						$content = file_get_contents($dir . $file);
						$lang[] .= $content;
					}
				}
			}
			
			closedir($handle);
			return $lang;
		}
 	}
}
Here is the lang file:

Code: Select all

<?php
//------------------------------------------------------------------------------
// File         : lang.php
// Description  : Contains language strings for phpSCMS.
// Author       : Sphen001 (Nick Ross)
// Date Started : Jan 28, 05
// Support      : http://phpscms.sourceforge.net
// Contact      : Sphen001 - at - users - dot - sourceforge - dot - net
// Copyright    : 2005 phpSCMS Group
// $Id: lang.php,v 1.1 2005/03/01 22:07:40 sphen001 Exp $
//------------------------------------------------------------------------------

if (empty($lang) || !is_array($lang))
{
	$lang = array();
}

$lang[] += array(
	'H_Nav_Links' => 'Nav Links',
	'H_Nav_Login' => 'Login',
	'H_Nav_Logout' => 'Logout',
	'H_Nav_Administrator' => 'Administrator',
	'H_Nav_UCP' => 'User Control Panel',
	'H_Nav_User' => 'User Actions',
	'UCP' => 'User Control Panel',
	'Admin_Link' => 'Admin Panel',
	'Register' => 'Register',
	'Wrong_Info' => 'Your information did not match a user in the database.  Please try again.',
	'Logged_On' => 'You are now logged on.',
	'Logged_Off' => 'You are now logged off.',
	'H_UCP' => 'User Control Panel',
	'H_Edit_Profile' => 'Edit Profile',
	'Change_Profile' => 'Edit your profile',
	'Change_Profile_Explain' => 'Edit your personal information here',
	'Edit_Profile_Explain' => 'Use this form to edit your personal information',
	'Profile_Updated' => 'Your profile has been updated successfully.',
	'Register' => 'Register',
	'Register_Explain' => 'Use this form to register.  All fields are required.',
	'Successfully_Registered' => 'You have been successfully registered.  You can now log in.',
	'Username_Already_Used' => 'The username you selected is already in use.  Please select another.',
	'No_Password_Match' => 'Your passwords did not match.  Please try again.',
	'Member_Username' => 'Member Username',
	'Joined_Date' => 'Joined Date',
	'Usergroup' => 'Usergroup',
	'H_Viewprofile' => 'Viewing Member Profile - ',
	'Welcome' => 'Welcome to phpSCMS',
	'Welcome_Text' => 'This is an example page for your CMS.  You don\'t need to keep this page.  You can delete it, or write your own code.  Details on how to use the templating system and other features can be found in the code.  If you don\'t want to use this page, you can delete it.  Just make sure you set a new default page in the Administration Control Panel.',
	'Welcome_Users' => 'This is part of the example in the code.  See the code for more details.  Number of users: ',
	'Categories' => 'Link Categories',
	'Links' => 'Links in this category',
	'Submitter' => 'Submitted by: ',
	'Submit_A_Link' => 'Submit A Link',
	'Submit_Link_Explain' => 'Use this form to submit a link to the database.  All fields are required.',
	'No_Links' => 'There are no links in this category.',
	'No_Info' => 'There was not enough data passed to this page.  Please contact an administrator.',
	'Submit_Links' => 'Submit Links',
	'Submit_News' => 'Submit News',
	'Forum_Name' => 'Forum Name',
	'Forum_Topics' => 'Topics',
	'Forum_Posts' => 'Posts',
	'Last_Post' => 'Last Post',
	'No_Posts' => 'No Posts',
	'Page_Time' => 'Page Time: ',
	'Queries' => 'Queries: ',
	'Memberlist' => 'Memberlist',
	'F_Username' => 'Username: ',
	'F_Password' => 'Password: ',
	'F_Password_Confirm' => 'Confirm Password: ',
	'F_Link_URL' => 'Link URL: ',
	'F_Link_Name' => 'Link Name: ',
	'F_Link_Desc' => 'Link Description: ',
	'B_Submit' => 'Submit',
	'B_Reset' => 'Reset',
	'B_Logout' => 'Logout'
);
?>
And here is the function call:

Code: Select all

$lang = getLangFile('./language/' . $config['language'] . '/');
However, it does not work as expected.

Basically, each lang file contains a $lang array. The function is supposed to open the files and add the contents of that $lang array to the master $lang array, which is then made available in the other files. This way, the language strings in new files uploaded to that directory are automatically available.

Can anyone help me with this?

Thank You

Sphen001
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

file_get_contents will not help much.. you just require()/include() it. Nothing more. i.e. In your linked file, remove line 28.

Switch $lang[] to $lang in your lang.php just posted.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oh sorry I had misunderstood you. I thought you just wanted the file contents as a string 8O
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Okay,

I was hoping I wouldn't have to require it, but I'll look at putting the require() in a loop.

Thanks for all of your help.

Sphen001
Post Reply