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!
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.
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]
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
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.
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:
pass additional argument to your function
move the variable out of the single quotes where it's used literally
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