Display all files in a directory php
What would be the best way to list all the files in one directory with PHP? Is there a $_SERVER function to do this? I would like to list all the files in the usernames/ directory and loop over that result with a link, so that I can just click the hyperlink of the filename to get there. Thanks! Show asked Apr 2, 2013 at 21:23
7 You are looking for the command scandir.
Following code will remove
answered Apr 2, 2013 at 21:26
miahmiah 9,6383 gold badges19 silver badges32 bronze badges 8 Check this out : readdir() This bit of code should list all entries in a certain directory:
Edit: miah's solution is much more elegant than mine, you should use his solution instead. answered Apr 2, 2013 at 21:26
Orel BitonOrel Biton 3,3782 gold badges14 silver badges14 bronze badges 4 (PHP 5, PHP 7, PHP 8) scandir — List files and directories inside the specified path Descriptionscandir(string Parametersdirectory The directory that will be scanned. sorting_order By default, the sorted order is alphabetical in ascending order. If the optional context For a description of the Return Values Returns an array of filenames on success, or Changelog
ExamplesExample #1 A simple scandir() example
The above example will output something similar to: Array ( [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ) Array ( [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . ) NotesTip A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide. See Also
dwieeb at gmail dot com ¶ 10 years ago
coolbikram0 at gmail dot com ¶ 10 months ago
$array as &$item) { mmda dot nl at gmail dot com ¶ 9 years ago
$result;
info at remark dot no ¶ 4 years ago
eep2004 at ukr dot net ¶ 7 years ago
kodlee at kodleeshare dot net ¶ 10 years ago
gambit_642 AT hotmailDOTcom ¶ 8 years ago
verifyPaths($paths){ csaba at alum dot mit dot edu ¶ 17 years ago
Pawel Dlugosz ¶ 17 years ago
= '/somedir'; Stan P. van de Burgt ¶ 17 years ago
artmanniako at gmail dot com ¶ 3 years ago
fazle dot elahee at gmail dot com ¶ 10 years ago
carneiro at isharelife dot com dot br ¶ 10 years ago
phpdotnet at lavavortex dot com ¶ 13 years ago
?>
Nerbert ¶ 3 years ago
fatpratmatt dot at dot gmail dot com ¶ 14 years ago
simon dot riget at gmail dot com ¶ 6 years ago
$list; telefoontoestel59 at hotmail dot com ¶ 7 years ago
Tom ¶ 7 years ago
$scan_result as $key => $value ) { Patrick ¶ 8 years ago
2bbasic at gmail dot com ¶ 10 years ago
moik78 at gmail dot com ¶ 12 years ago
SPekary ¶ 5 years ago
magicmind at netcabo dot pt ¶ 13 years ago
progrium+php at gmail dot com ¶ 17 years ago
dsiembab at fullchannel dot net ¶ 13 years ago
beingmrkenny at gmail dot com ¶ 16 years ago
recursive_subfolders($folders) {// Set path here hex at mail dot nnov dot ru ¶ 9 years ago
webmaster at gmail dot com ¶ 2 years ago
phpnet at novaclic dot com ¶ 12 years ago
cHH ¶ 14 years ago
$directories as $value)
boen dot robot at gmail dot com ¶ 12 years ago
admin at kmoz000 dot free ¶ 4 years ago
asamir at asamir dot net ¶ 14 years ago
in_array($ext, $allowext)) { csaba at alum dot mit dot edu ¶ 17 years ago
djacobson at usa dot rugby ¶ 2 years ago
wwwebdesigner at web doott de ¶ 17 years ago
What is __ DIR __ in PHP?The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file.
How do I view a file in PHP?There are different functions that allow you to read all file data, read data line by line and read data character by character. The available PHP file read functions are given below.. $fp = fopen("c:\\file1.txt", "r");//open file in read mode.. while(!feof($fp)) {. echo fgetc($fp);. fclose($fp);. What is PHP glob?The glob() function returns an array of filenames or directories matching a specified pattern.
Is a directory PHP?The is_dir() function in PHP used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else it returns False.
|