getcwd – PHP Reference Book Blog https://phpreferencebook.com/ PHP Reference: Beginner to Intermediate PHP5 Sun, 01 Oct 2017 17:21:19 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.13 Quick Tip: getcwd() for Contents of Current Directory https://phpreferencebook.com/tips/getcwd-scandir-get-display-contents-current-directory/ Tue, 22 Dec 2009 14:00:52 +0000 https://phpreferencebook.com/?p=352 Continue reading Quick Tip: getcwd() for Contents of Current Directory]]> The getcwd() function is short for ‘GET Current Working Directory’. This can easily be combined with the scandir() function which returns an array of all the files and directories inside the specified directory. This tip was excluded from the book as an oversight.
 
A quick way to get a list of all the contents of the current directory is to use the following code:

function preprint($arr){
  echo '< pre>'.print_r($arr).'< /pre>';
}
$array = scandir(getcwd());
preprint($array); // nicely formatted display of the array

 
Of course, you can skip the print/echo portion if you don’t wish to display the contents and just use the array to perform other checks, but you get the idea.

]]>