Specifying variables in a path

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
phillipjz
Forum Newbie
Posts: 2
Joined: Wed Jan 03, 2007 1:17 pm

Specifying variables in a path

Post by phillipjz »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am relatively new to php and am running int a problem I can't seem to fins and answer to. I need to count the files in a directory. However, the directory will be a variable dependent on the page the user is on. If I hardcode a directory name into the path works, but when I place the variable int the path, it does not work.

I've tried every variaition I can think of. I'd hate to scrap this code as its so close to working. Any help would be hugely appreciated.

Code: Select all

function num_files($directory='.') {
return count(glob($directory.'/_images/_projects/$project/*'));
}
$count = num_files();

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Is this what you're looking for? :

Code: Select all

function num_files($directory='.') {
   return count(glob($directory.'/_images/_projects/$project/*'));
}
$count = num_files(); // number of files in the ./_images/_projects/$project directory
$count = num_files('some_other_directory'); // number of files in the some_other_directory/_images/_projects/$project directory
phillipjz
Forum Newbie
Posts: 2
Joined: Wed Jan 03, 2007 1:17 pm

Post by phillipjz »

sorry about that post. first time positing. i'll read tthe guidelines shortly. anyway i'm looking for the number of files in the ./_images/_projects/$project directory where $project is a variable.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

phillipjz wrote:sorry about that post. first time positing. i'll read tthe guidelines shortly. anyway i'm looking for the number of files in the ./_images/_projects/$project directory where $project is a variable.
so you would:
  1. pass additional argument to your function
  2. move the variable out of the single quotes where it's used literally

Code: Select all

function num_files($project, $directory='.') {
   return count(glob($directory.'/_images/_projects/' . $project . '/*'));
}
$count = num_files('project1'); // number of files in the ./_images/_projects/project1 directory
$count = num_files('project2', 'some_other_directory'); // number of files in the some_other_directory/_images/_projects/project2 directory
Post Reply