Mutley Posted September 16, 2006 Share Posted September 16, 2006 Is it possible so when you create a new folder on your server PHP can detect it and display the name of it? Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/ Share on other sites More sharing options...
Orio Posted September 16, 2006 Share Posted September 16, 2006 What do you mean?Can you give an example maybe?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-92932 Share on other sites More sharing options...
Mutley Posted September 16, 2006 Author Share Posted September 16, 2006 I create a folder using my FTP called "bob". Then on the website, it displays a link to the folder "bob". Like www.mysite.com/bob/ as a hyperlink.So everytime a new folder is created, it is detected and displays it. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-93026 Share on other sites More sharing options...
Daniel0 Posted September 16, 2006 Share Posted September 16, 2006 Here is an example: [code]<?phpheader("Content-type: text/plain");function read_contents($directory='.'){ if(substr($directory,-1) != '/') { $directory .= "/"; } $contents = @scandir($directory); if(is_array($contents)) { foreach($contents as $item) { if($item != '.' && $item != '..') { if(is_dir($directory.$item)) { echo "{$directory}{$item}\n"; } } } }}read_contents("/home/daniel");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-93050 Share on other sites More sharing options...
Mutley Posted September 17, 2006 Author Share Posted September 17, 2006 Doesn't seem to work, nothing appears.At the end I put:read_contents("/");I thought that would show all the folders in the directory the file is in? Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-93541 Share on other sites More sharing options...
Orio Posted September 17, 2006 Share Posted September 17, 2006 Are you running PHP5 or greater? The function Daniel0 made for you uses scandir() which is only available since PHP5.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-93615 Share on other sites More sharing options...
Mutley Posted September 18, 2006 Author Share Posted September 18, 2006 Gah, I've tried a few different methods but can't get that script to work, it just opens a blank page, no source code.I've tried different directories and all sorts. Yes I'm sure I am on PHP5. Does it work for others? Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-93838 Share on other sites More sharing options...
Mutley Posted September 18, 2006 Author Share Posted September 18, 2006 I really need this to be working. If someone can give me a working code and a step by step quote of what each part does, it would mean alot.Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94009 Share on other sites More sharing options...
Orio Posted September 18, 2006 Share Posted September 18, 2006 Try removing the "@" sign before "scandir()".Also add in the begining of the script error_reporting(E_ALL).Maybe this will help us to find the source of the problem.orio. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94012 Share on other sites More sharing options...
Mutley Posted September 18, 2006 Author Share Posted September 18, 2006 Nope:Fatal error</b>: Call to undefined function: scandir() on line 12That's with removing the "@" with it left on, it doesn't appear any errors, just blank page. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94017 Share on other sites More sharing options...
AndyB Posted September 18, 2006 Share Posted September 18, 2006 http://ca3.php.net/manual/en/function.scandir.phpscandir is php5 only. The manual reference linked above shows examples of equivalent functionality with php4 Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94019 Share on other sites More sharing options...
Orio Posted September 18, 2006 Share Posted September 18, 2006 So you dont have scandir() => You have PHP<5...Add this to your code, and it should work:[code]<?phpif(!function_exists('scandir')) { function scandir($dir, $sortorder = 0) { if(is_dir($dir)) { $dirlist = opendir($dir); while( ($file = readdir($dirlist)) !== false) { if(!is_dir($file)) { $files[] = $file; } } ($sortorder == 0) ? asort($files) : rsort($files); // arsort was replaced with rsort return $files; } else { return FALSE; break; } }}?>[/code]** Taken from [url=http://www.php.net/manual/en/function.scandir.php]scandir()[/url], user's notes.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94020 Share on other sites More sharing options...
Mutley Posted September 18, 2006 Author Share Posted September 18, 2006 @ OrioWhere do I add that code or do I use just that code? How do I use the function to chose which directory I'm wanting the folders to be listed from? Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94021 Share on other sites More sharing options...
Orio Posted September 18, 2006 Share Posted September 18, 2006 The code should look like:[code]<?phpif(!function_exists('scandir')) { function scandir($dir, $sortorder = 0) { if(is_dir($dir)) { $dirlist = opendir($dir); while( ($file = readdir($dirlist)) !== false) { if(!is_dir($file)) { $files[] = $file; } } ($sortorder == 0) ? asort($files) : rsort($files); return $files; } else { return FALSE; break; } }}function read_contents($directory='.'){ if(substr($directory,-1) != '/') { $directory .= "/"; } $contents = @scandir($directory); if(is_array($contents)) { foreach($contents as $item) { if($item != '.' && $item != '..') { if(is_dir($directory.$item)) { echo "{$directory}{$item}\n"; } } } }}read_contents($some_dir);?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94027 Share on other sites More sharing options...
obsidian Posted September 18, 2006 Share Posted September 18, 2006 one more question: are you looking for the function to be recursive? i mean, if you have a new folder created, and you create folders within that folder, are you wanting to display them all on the page? if so, try something like this:[code]<?phpfunction getFileStructure($dir = './') { $out = "<ul>\n"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file != '.' && $file != '..') { if (is_dir($dir . $file)) { $out .= "<li>{$dir}{$file}</li>\n"; $out .= getFileStructure($dir . $file . '/'); } } } } closedir($dh); } $out .= "</ul>\n"; if ($out == "<ul>\n</ul>\n") $out = ''; return $out;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94045 Share on other sites More sharing options...
Mutley Posted September 18, 2006 Author Share Posted September 18, 2006 Thanks alot they work. :)Finally, this has been bugging me all week! Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94047 Share on other sites More sharing options...
Mutley Posted September 19, 2006 Author Share Posted September 19, 2006 If I have a folder called "Yes and no" for example, with spaces, how do I make it display as "Yes and no" in the list but work as a link when you click it? It can't find a folder called "Yes and no" because it should have %20 in I think.Maybe something like "Yes-and-no" but the list filters out the - so it looks like "Yes and no" but the link still stays as "Yes-and-no". If you understand me? Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94577 Share on other sites More sharing options...
Orio Posted September 19, 2006 Share Posted September 19, 2006 I think it'll automaticly work if you have the "href" part in quotes (IE <a href="folder name/"> and not <a href=folder name/>) and a slash ("/") in the end...Orio. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-94592 Share on other sites More sharing options...
Mutley Posted September 24, 2006 Author Share Posted September 24, 2006 The problem is Orio, I do it like this using varialbes, I already have quotes:[code]if(is_dir($directory.$item)) { $team = $_GET['team']; ?> <p><a href="teamsgallery3.php?team=<?=$team?>&subject=2006&game=<?=$item?>"><?=$item?></a> - <? include("gallery/images/".$team."/2006/".$item."/date/date.txt");?></p><? } } } }}read_contents("gallery/images/".$team."/2006");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-97590 Share on other sites More sharing options...
Mutley Posted September 24, 2006 Author Share Posted September 24, 2006 Can anyone see how I fix this problem? Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-97633 Share on other sites More sharing options...
Daniel0 Posted September 24, 2006 Share Posted September 24, 2006 Do something like this:[code]<?php$folder_name = "Yes and no";echo "<a href='file.php?folder=".urlencode($folder_name)."'>{$folder_name}</a>";?>[/code]That will output: [code]<a href='file.php?folder=Yes+and+no'>Yes and no</a>[/code]Then you can just urldecode it on the other page. Quote Link to comment https://forums.phpfreaks.com/topic/20958-list-folders-via-php/#findComment-97664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.