Solved

Variable inside function

Posted on 2011-03-17
4
255 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
ID: 35155893
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
ID: 35155967
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
ID: 35155982
<?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
ID: 35156007
Perfect, thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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