Solved

Variable inside function

Posted on 2011-03-17
4
239 Views
Last Modified: 2012-06-21
I have a function which lists files in a directory, the first line of my function is as follows:

function listdirectory($c = 'docs/r0jd38dd', $wcwd = false) {

$c is the directory to list. I now want it so that I can pass the folder to the function as the directory will change depending on the page. For this I've tried:

$bookid = 'docs/' . $_GET['id'];
function listdirectory($c = $bookid, $wcwd = false) {

but it doesn't work, where am I going wrong?

Thanks
0
Comment
Question by:djfenom
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
Comment Utility
Functions work like this:

function listdirectory($c, $wcwd) {
    echo $c . '<br />;
    echo $wcwd . '<br />;
}

$bookid = 'docs/' . $_GET['id'];
listdirectory($bookid, false);

Open in new window


And check the manual for some more explanation:
http://php.net/manual/en/language.functions.php
0
 

Author Comment

by:djfenom
Comment Utility
Hmmm, this is my full function, how would I put it in?
<?php
  $bookid = 'docs/r0jd' . substr($_GET['id'],-2) .'dd';
  function listdirectory($c = $bookid, $wcwd = false) {

                if($wcwd === false)

                  $wcwd = substr($wcwd = $_SERVER['REQUEST_URI'], 0, strrpos($wcwd, '/') + 1);

 

                echo('<ul class="dirlist">');

                $arr = scandir($c);
	        asort( $arr );
                foreach( $arr as $f ) {
				if ($f != 'User guides') {
                  if(strpos($f, '.') === 0) continue;

 

                  $ff = $c . '/' . $f;

                  $folder = substr($c, 14);

                  if(!is_dir($ff)) {

                                  if ($f == 'Thumbs.db' || $f == 'hr_handbook.zip') {

                                  }

                                  else {

                                                  echo '<li><a href="/docs/?file=' . $folder . '/' . $f . '" class="docdl">' . $f . '</a>';

                                  }

                  }

                  else {

                                        echo '<li><strong>' . $f . '</strong>';

                  }

                  if(is_dir($ff)) listdirectory($ff, $wcwd);

                  echo '</li>';

                }

				}
                echo('</ul>');
  } 

listdirectory();
?>

Open in new window

0
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 500 total points
Comment Utility
<?php
  
  function listdirectory($c, $wcwd) {

                if($wcwd === false)

                  $wcwd = substr($wcwd = $_SERVER['REQUEST_URI'], 0, strrpos($wcwd, '/') + 1);

 

                echo('<ul class="dirlist">');

                $arr = scandir($c);
	        asort( $arr );
                foreach( $arr as $f ) {
				if ($f != 'User guides') {
                  if(strpos($f, '.') === 0) continue;

 

                  $ff = $c . '/' . $f;

                  $folder = substr($c, 14);

                  if(!is_dir($ff)) {

                                  if ($f == 'Thumbs.db' || $f == 'hr_handbook.zip') {

                                  }

                                  else {

                                                  echo '<li><a href="/docs/?file=' . $folder . '/' . $f . '" class="docdl">' . $f . '</a>';

                                  }

                  }

                  else {

                                        echo '<li><strong>' . $f . '</strong>';

                  }

                  if(is_dir($ff)) listdirectory($ff, $wcwd);

                  echo '</li>';

                }

				}
                echo('</ul>');
  } 

$bookid = 'docs/r0jd' . substr($_GET['id'],-2) .'dd';
listdirectory($bookid, false);
?>

Open in new window

0
 

Author Comment

by:djfenom
Comment Utility
Perfect, thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now