Page 1 of 1

Specifying variables in a path

Posted: Wed Jan 03, 2007 1:34 pm
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]

Posted: Wed Jan 03, 2007 1:43 pm
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

Posted: Wed Jan 03, 2007 1:52 pm
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.

Posted: Wed Jan 03, 2007 2:08 pm
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