PHP - How Do I List Folders/files Alphabetically Help!!!
Hi
Sorry if this is the wrong place for this but I'm totally stumped here and I know its a basic question but I'm very new to php and I need help :-) Basically all i would like to do is list the files and folders in the following directory alphabetically, I know i need to put them in an array etc but I don't know how and would really appreciate some help! So any help would be very gratefully received. Here's my code so far: <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Documents'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> Many thanks in advance! Similar TutorialsI'm pretty new to php and I'm furiously trying to get this code to list the folders/files alphabetically but to no avail! I'd really appreciate it if someone could take a look and guide me as to what to do, I'm pulling what little hair i have left out! I can currently see the folder contents but the order is all over the place. Thanks very much!!! -------------------------- <?php $sub = ($_GET['dir']); if (strstr($sub, "..")) {$sub = "";} $path = '../media/Movies'; $path = $path . "$sub"; $dh = opendir($path); $i=1; while (($file = readdir($dh)) !== false) { if(substr($file,0,1) != ".") { if (substr($file, -4, -3) =="."){ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. $file <br />"; }else{ echo "<font size=2 face=\"Arial, Helvetica, sans-serif\">$i. <a href='?dir=$sub/$file'>$file</a><br />"; } $i++; } } closedir($dh); ?> -------------------------- Hey guys, I'm very new to PHP so I apologize for any stupid mistakes or if I'm a little slow understanding things. I have put together a small website and I wanted to use a PHP script to display a list of files and folders (and sub folders). Basically a ghetto equivalent of an FTP page so I can apply CSS and make things look a little pretty. I have the following code I found from Google that will display all of the files and folders, not perfect but it will work for now! However I can't seem to work out the syntax to make them a hyperlink. Any help would be appreciated! <?php function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories to ignore when listing output. Many hosts // will deny PHP access to the cgi-bin. $dh = @opendir( $path ); // Open the directory to the handle $dh while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory if( !in_array( $file, $ignore ) ){ // Check that this file is not to be ignored $spaces = str_repeat( ' ', ( $level * 4 ) ); // Just to add spacing to the list, to better // show the directory tree. if( is_dir( "$path/$file" ) ){ // Its a directory, so we need to keep reading down... echo "<strong>$spaces $file</strong><br />"; getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { echo "$spaces $file<br />"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle } ?> I came accross this code on YouTube and it works great for listing the content of a folder that the .php file is in. However, how can I alter this code to list files of all folders and sub-folders? I thought plugging in an asterik might work on the "$path =" line, but it didn't. Code: [Select] <?php $path = "."; $handle = opendir($path); while ($file = readdir($handle)) { echo $file . "<br>"; } closedir($handle); ?> Hi, I have coded a site, whereby users can log in, and depending on their group, view all files within their group directory: Code: [Select] if ($handle = opendir("storage/Admin/$group")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { echo "<a target=\"_blank\" href=\"storage/Admin/$group/$file\">$file</a> <br />"; } } closedir($handle); } In this example, a user can click on their file, and it will let them download it, but if there is a folder (directory) in there, once clicked on, it will open a index of the directory. Is there a way instead, to let it open a custom page and list the files within it there? i have some questions on how folders and files permissions work. say i have users directories outside 'protected' as below.. Code: [Select] users -- usera -- docs -- userb -- docs protected i do not want user B who does not have the rights, to access anything in user A directories. also, i do not want any person to access the directories directory via url links. basically i just want users to be able to access their own directories, and no one else. how can it be done? thanks! hello, I have implemented a snippet that reads images in I create folders dynamically from admin and makes them see lightbox style .. the same works fine but it shows all images in all folders and not those relating to folder clicked .. The result you can see it he aires-restauro.org/beta/gallery2.php code: <?php $folder = opendir('gallery/'); echo "<gallery>"; while ($file = readdir($folder)) { if($file != '.' && $file != '..'){ echo "<album path='gallery/$file/' name='$file'>"; $subfolder="gallery/".$file."/"; $subfolderdir = opendir($subfolder); while ($subfile = readdir($subfolderdir)) { if($subfile != '.' && $subfile != '..'){ echo "<a href='$subfolder$subfile' class='pirobox_gall'>$file</a>"; } } echo "</album>"; } } echo "</gallery>"; ?> repeats the same directory as I would like to see that by clicking the directory do see the images of that directory sorry but I do not speak English well and I thank you in advance hi all i am looking for the quickest way to zip all files and folder of a website i am currently using the followning. <?php $date = date("F-j-Y-g-ia"); if(exec("cd /home/isd/public_html/bk/files;tar -cvpzf backup-$date.tar /home/isd/public_html")) { echo "done"; } ?> This works but was just wondering if anyone can give me and insight on a better solution?? Thanks Hello I have a simple question about file handling... Is it possible to list all files in directories / subdirectories, and then read ALL files in those dirs, and put the content of their file into an array? Like this: array: [SomePath/test.php] = "All In this php file is being read by a new smart function!"; [SomePath/Weird/hello.txt = "Hello world. This is me and im just trying to get some help!";and so on, until no further files exists in that rootdir. All my attempts went totally crazy and none of them works... therefore i need to ask you for help. Do you have any ideas how to do this? If so, how can I be able to do it? Thanks in Advance, pros Hi im using ftp_rawlist () to list files in my web hosting folder /public_html/ it works fine, but is there anyway to only show/list .php & html files? I am trying to find a script that will show just the file names of files in a specific directory. I tried this code posted previously on this forum and it works but shows the directory name. Code: [Select] <?php filesInDir('attachments'); function filesInDir($tdir) { $dirs = scandir($tdir); foreach($dirs as $file) { if (($file == '.')||($file == '..')) { } elseif (is_dir($tdir.'/'.$file)) { filesInDir($tdir.'/'.$file); } else { echo $tdir.'/'.$file."<br>"; } } } ?> Result is: attachments/Sedona_and_Slot_Canyon_links.docx Thanks for the help. Hi, I'm automating a process of Inserting a CSV to a database. I'm doing it in PHP. I need to go to http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/ and get the list of the files in this directory. Which is the best way of doing it in PHP? Best Regards, I have a script that works perfectly, for one type of file extension. What I want to do now is be able to list all files in a set of file types. I want to pass the argument to the fileSystem class's loadFiles method like this: $some_var = $fileSystem->loadFiles(DATA_DIRECTORY, "csv,xls,etc"); Then in the loadFiles method it reads the dir for each file, then test its extension. If they are of the right type, then push it on the returned array. Now I am having problems testing if the file's extension is in the array. If I use if (strpos(getExt($file), $array_of_wanted_exts))... php would match php3.. Does anybody have a better method? Hello everyone I have two dropdown lists. First one you can see the tables of my db and the second one the files inside a folder. The problem is that i can't send my selections from the dropdown list to an other file. My project is to choose a table, then a CSV file and then load the file in db. index.php: Code: [Select] <?php //EMFANISH PINAKWN include("../forma/dbCon.php"); echo "<form action='load_table_test.php' method='POST'>"; $sql="SHOW TABLES FROM misthodosia"; $result=mysql_query($sql); if (!$result) { echo "Υπάρχει πρόβλημα με την βάση\n"; echo 'MySQL Error: ' . mysql_error(); exit; } echo"<select>"; while ($row = mysql_fetch_row($result)) { echo "<option name='{row[0]}' value='{row[0]}'>{$row[0]}</option>"; } echo"</select>"; mysql_free_result($result); //==========================EMFANISH ARXEIWN========================== function populateDropdown(){ $path = glob("C:\\xampp\\htdocs\\dbadmin\\upload_files\\*"); //the double \\ is not a typo you //have to escape it //if the path cannot be found if(!($path)){ echo("Δεν υπάρχει ο φάκελος"); return; }//if //the path is valid else{ foreach($path as $k){ $i = basename($k); echo "<option name='$i'>".$i."</option>"; }//foreach }//else }//populateDropdown echo"<select>"; populateDropdown(); echo"</select> "; echo"<input type='submit' name='submit' value='Φόρτωσε1111111' />"; echo "</form>"; $hello="hello"; ?> load_table_test.php: Code: [Select] <?php include("../forma/dbCon.php"); echo $_POST['hello']; echo $_POST['{row[0]}']; echo $i; //$pinakas=$_POST['pinakas'] //$arxeio=$_POST['arxeio'] //$pinakas=$_POST['row'] //$arxeio=$_POST['diadromi'] //mysql_query("LOAD DATA LOCAL INFILE "$arxeio" INTO TABLE $pinakas //FIELDS //TERMINATED BY ',' //ENCLOSED BY '\"' //LINES TERMINATED BY '\n') echo "$arxeio"; echo "$pinakas"; echo "<br/><br/><br/><br/><br/><a href='index.php'>Επιστροφή στην προηγούμενη σελίδα.</a>"; ?> errors: Code: [Select] Notice: Undefined index: hello in C:\xampp\htdocs\dbadmin\load_table_test.php on line 6 Notice: Undefined index: {row[0]} in C:\xampp\htdocs\dbadmin\load_table_test.php on line 8 Notice: Undefined variable: i in C:\xampp\htdocs\dbadmin\load_table_test.php on line 9 Notice: Undefined variable: arxeio in C:\xampp\htdocs\dbadmin\load_table_test.php on line 27 Notice: Undefined variable: pinakas in C:\xampp\htdocs\dbadmin\load_table_test.php on line 28 Hello. I have a directory with files that look like this: priceguide_20100809.pdf priceguide_20100808.pdf priceguide_20100807.pdf priceguide_20100806.pdf I am trying to come up with a function that will grab and save the filename with the biggest date .. ie: priceguide_20100809.pdf. Any assistance would be greatly appreciated!! Hello Coder Guys, I need a small help from you. I want to list all files & directories based on last file modification time of server. I want to list new files as first & old files as last. Consider "files" as the directory name which contains all files & directories. Please make the php code to display all files & directories from "files" directory based on last file modification time of server. Thanks IN Advance ! I have a folder containing files. I want to replace some characters in the file names (e.g. "_" with "-"), then make a list of new files (renamed ones). Thanks Hello everyone. Newbie question. I have a page that pulls listings from a database by letter (a,b,c, etc) , but I would like to have a Next button, that would take people to the next letter, but can't figure out how to make that happen. I understand doing that with numbers and using the +1 for the next page, but how do you code a next button to search for the next letter? Any help would be VERY appreciated. I am a php newb. I have 2 somewhat simple problems. First I have a huge list of movies that I would like sorted in a drop down list alphabetically. I already have the drop down list made and the movies show up correctly. But I cannot figure out how to sort the list. Here is the code <?php $dir="/backup/Movies"; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.php") { $thelist .= '<option>'.$file.'</option>'; } } closedir($handle); } ?> <form action="listen.php" method="post" name="table"> <select name="Movie"> <P><?=$thelist?></p>' </select> </form> Second problem is inside this directory there are a few other directories is it possible to make the directories show at the top instead of them being sorted alphabetically with the movies. Thanks in advance Jake show list of files uploaded by current session user to the database. I want to show different users when they log in to the website...they can see a list of old files that they have uploaded. can anyone tell me the code/script to this.....please, ty I have a 'user' table and a display users page. How would I display them in alphabetical order by their username? Heres my current basic display page: <?php include 'dbwire.php'; include 'header.php'; $query = mysql_query('SELECT * FROM user'); while ($row = mysql_fetch_array($query)) { echo '<b>Username:</b> ' . $row['user'] . '<br />'; echo '<b>Real Name:</b> ' . $row['name'] . '<br />'; echo '<b>Email:</b> ' . $row['email'] . '<br />'; echo '<b>Location:</b> ' . $row['location'] . '<br /> <hr>'; } ?> Theres also an id row but since user wouldn't be added alphabetically I wouldn't be able to order them by id. |