PHP - Readdir / Scandir - Formats Of Filenames?
Hi all I hope I have come to the right place. My system reads files stored on a drive and lists them to users through plain HTML, I made it 11 years ago and have to refresh my memory. My problem is that filenames seem to come in different formats, to how to decode/encode them is an issue...
My users use Scandinavian letters (æøåäöüõ) and it seems like one filename is in one format and another in another format. There is no logic to what format the filenames comes it. All files are ok and downloads as they should, they just dont list well. Any idea how I can handle this issue?
Similar TutorialsHi all I am using this function to list all files and folders within a directory Code: [Select] function getFilesFromDir($dir) { $files = array(); if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(is_dir($dir.'/'.$file)) { $dir2 = $dir.'/'.$file; $files[] = getFilesFromDir($dir2); } else { $files[] = $dir.'/'.$file; } } } closedir($handle); } return array_flat($files); } function array_flat($array) { foreach($array as $a) { if(is_array($a)) { $tmp = array_merge($tmp, array_flat($a)); } else { $tmp[] = $a; } } return $tmp; } When I call the function I give the dir as a variable, however if the dir I want is a sub-dir of a current dir then the list starts from the main dir Basically say my file test.html is contained here files/test/test.html, the input I need to give is files/test/. This would then output in the array ->files/test/test.html. What I would like the output to be is test/test.html. How do I edit my recursive function to start from the final folder in the input? Thanks Hi all. I'm trying to build a dynamic multiple tiered navigation menu that operates on the existence of directories. So far, I have some code that will read through the document root, and one sub level of directories but I'm not sure how to get into the third level of directories. Here's what I have so far... The code: $docroot=scandir($_SERVER['DOCUMENT_ROOT']); unset($docroot[array_search('.',$docroot)]); unset($docroot[array_search('..',$docroot)]); unset($docroot[array_search('php',$docroot)]); unset($docroot[array_search('css',$docroot)]); unset($docroot[array_search('input_forms',$docroot)]); $docroot=array_values($docroot); //Build tier 1 echo "<div class=\"tier\">"; echo "<a href=\"/index.php\">Home</a>"; foreach($docroot as $value1){ if(is_dir($value1)){ $t2=scandir($_SERVER['DOCUMENT_ROOT'] . "/" . $value1); unset($t2[array_search('.',$t2)]); unset($t2[array_search('..',$t2)]); $t2=array_values($t2); $t1[]=$t2; echo "<a href=\"/$value1/\">"; $value1=str_replace("_"," ",$value1); $value1=ucwords($value1); echo $value1; echo "</a>"; } } echo "</div>"; The output: Code: [Select] [it automatically builds the first tier of the menu with some stylesheet definitions and places it here. I did this just for testing purposes.] Array ( [0] => Array ( [0] => bus_mgmt_context.pdf [1] => financial_management [2] => index.php [3] => nav.php [4] => service_level_management [5] => service_portfolio [6] => workforce_management ) [1] => Array ( [0] => element_inventory [1] => index.php [2] => mobile_administration [3] => monthly_bill_review [4] => nav.php ) [2] => Array ( [0] => business_it_requests [1] => change_management [2] => demand_management_context.pdf [3] => index.php [4] => nav.php [5] => project_management [6] => release_management ) [3] => Array ( [0] => availability_management [1] => capacity_management [2] => configuration_management [3] => index.php [4] => nav.php [5] => planning [6] => problem_management [7] => system_planning ) [4] => Array ( [0] => context [1] => input [2] => reports [3] => sop [4] => t3.php [5] => video ) [5] => Array ( [0] => executive_reports [1] => index.php [2] => kpi_index [3] => nav.php ) [6] => Array ( [0] => compliance [1] => hipaa_definitions.pdf [2] => index.php [3] => information_security [4] => nav.php [5] => risk_context.pdf [6] => service_continuity [7] => threat_management ) [7] => Array ( [0] => incident_management [1] => index.php [2] => nav.php [3] => release_management [4] => service_desk [5] => service_requests [6] => supplier_management ) ) Sorry for the noobie question, I am just a beginner still. I am trying to use the readdir function to echo back all the contents of a folder. The directory that I want to read back has 31 empty folders. I am able to get them to read back, but they are in a random order and there are two additional files at the top named "." and ".." -- any idea how to fix these? Here is my code and pictures: Quote $path = "c:/test/"; if(is_dir($path)) { if($dir_handle = opendir($path)) { while($filename = readdir($dir_handle)) { echo "filename: {$filename}<br>"; } closedir($dir_handle); } } Hi folks, First time posting here and a bit of a novice at PHP, but am learning.... I am fiddling with a small bit of code to create a very basic php gallery (I'm sure everyone has at some point). I started off with a mysql intergrated one then thought I would try a new one without the mysql. Its working fine but I am just having one very strange problem. Basically, I started of with just /mygallery/index.php?w=... where ... = the location (physical folder) of photos. I use opendir to open the contents of w= and parse whether it is a directory or a file (then narrow it down to just images). If it is a dir, then I present it as a link to that folder (sub-album) if it is a photo, I present a thumb link to the image. Now I have added a mod_rewrite rule to allow my urls to be of the form 'mygallery/album/subalbum' this gets ocnverted into 'mygallery/index.php?w=album/subalbum' Now the funny bit. If I create a new folder in one of the subalbum, e.g. : mygallery/album/subalbum/<b>New Folder</b> the code works. i.e. when I point my browser at 'mygallery/album/subalbum' I get a link showing 'New Folder' But if I create a new folder of any other NAME it does not appear in the list!! So if I just rename 'New Folder' to 'New Folde' it drops of the web page!!! Now I am really not sure what the hell is going on... Hope someone can help. I will post my code if necessary. Thanks in advance! Jon Reynolds Hey guys basically with a simple script that I made I need some help with it's doing my head in. What i'm trying to do is allow multiple filetypes to be shown by readdir, i want both .zip and .rar to be in the list, not just .zip how would i add .rar in this method as well to be visible on readdir? <?php $dirname = "/home/test/"; $dir = opendir($dirname); $extension = array_pop(explode(".", $file)); ?> <?php while(false != ($file = readdir($dir))) { if ($file != "." && $file != ".." && array_pop(explode(".", $file)) == "zip") { echo(" <b>» <a href='$dirname$file'>$file - Download</a> <br /> </b> "); } } ?> Hi all, I have a PHP function that reads JPG's from a folder that contains over 100,000 files. The problem I have is that the READDIR() function takes a while to list the contents due to the large number of files. I'm trying to speed up the process by using a filter and listing only the files I need. Example: list files with a timestamp for an entered date period. Code: [Select] $file >= '2012-04-02 OR $file <= '2012-03-04' This lists the JPG files but my modification to list files using a timestamp does not work nor can I find a way of seeing why it does not work. This has me stuck. Thank you in advance to anyone who can post some pointers. Code that lists JPGS only Code: [Select] <?php $path = 'imgs/'; function directory($dir,$filters) { $handle=opendir($dir); $files=array(); if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}} // 'all' filter is if you set to list ALL files and folders. if ($filters != "all") { $filters=explode(",",$filters); while (($file = readdir($handle))!==false) { for ($f=0; $f < sizeof($filters); $f++): $system=explode(".",$file); if ($system[1] == $filters[$f]){$files[] = $file;} endfor; } } closedir($handle); return $files; } $files = directory($path, "jpg"); //for multiple file types just separate the file extensions by commas foreach ($files as $value) { echo $value.'<br>'; } ?> I have added the filter Code: [Select] && filemtime($file) >= strtotime('2010-04-12') but it does not work. Code: [Select] <?php $path = 'imgs/'; function directory($dir,$filters) { $handle=opendir($dir); $files=array(); if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}} // 'all' filter is if you set to list ALL files and folders. if ($filters != "all") { $filters=explode(",",$filters); while (($file = readdir($handle) && filemtime($file) >= strtotime('2010-04-12'))!==false) { for ($f=0; $f < sizeof($filters); $f++): $system=explode(".",$file); if ($system[1] == $filters[$f]){$files[] = $file;} endfor; } } closedir($handle); return $files; } $files = directory($path, "jpg"); //for multiple file types just separate the file extensions by commas foreach ($files as $value) { echo $value.'<br>'; } ?> I've had a bit of a search around to see if this is possible but have yet to find something. I'm looking to be able to offset the results from opendir or similar function without returning all the files/folders. In other words i'd like to display the file/folder results over multiple pages when there are a lot of files/folders to display. Is this at all possible? My new host (Siteground) cloned and moved my html/php site (built by the previous owner) from Heart Internet server. All of the site works except for the sidebar, I get these 2 errors. Anyone shed any light on these at all?
Warning: readdir(): supplied argument is not a valid Directory resource in /home/customer/www/dunster.org.uk/public_html/include/toolbox.php on line 97
Warning: opendir(/home/sites/dunster.org.uk/public_html//local_information) [function.opendir]: failed to open dir: No such file or directory in /home/customer/www/dunster.org.uk/public_html/include/toolbox.php on line 86 I could use some help on naming my PHP files... The approach I have settled on for now is that I have some PHP page that does something (e.g. "change_password.php") and then another page to which the user is redirected (e.g. "change_password_results.php") which displays an outcome message like "Your Password has been re-set." Questions: 1.) Is it a problem having multi-word PHP File Names? 2.) Is it a problem separating them with underscores? 3.) Will it look *strange* to users looking at the URL to see a file name like "create_account_results.php" ?? 4.) Will this impact SEO? Thanks, Debbie Hi, I have the following situation: Code: [Select] Array ( [5] => GeoLiteCity_20090601.zip [6] => GeoLiteCity_20090701.zip [7] => GeoLiteCity_20090801.zip [8] => GeoLiteCity_20090901.zip [9] => GeoLiteCity_20091001.zip [10] => GeoLiteCity_20091101.zip [11] => GeoLiteCity_20091201.zip [12] => GeoLiteCity_20100101.zip [13] => GeoLiteCity_20100201.zip [14] => GeoLiteCity_20100301.zip [15] => GeoLiteCity_20100401.zip [16] => GeoLiteCity_20100501.zip [17] => GeoLiteCity_20100601.zip [18] => GeoLiteCity_20100701.zip [19] => GeoLiteCity_20100801.zip [20] => GeoLiteCity_20100901.zip [21] => GeoLiteCity_20101001.zip [22] => GeoLiteCity_20101101.zip [23] => GeoLiteCity_20101201.zip ) I have this array with filenames, the patern for the name of the file is NAME_DATE.zip I need to get from this array the most recent file(GeoLiteCity_20101201.zip). Which is the best way of doing this task? Give me some clues. Best Regards, Hi, I've tried various methods (ie. glob etc.) but haven't had any success, the purpose is to scan through all directories for php files, then place those php files along with their path ($dir/$filename) into an array (to use elsewhere) So if I have the following file/directory structure (example): Code: [Select] --forums: ---topic.php ---forum.php --func: ---functions.php Would produce the following array: $files = array('topic.php' => 'forums/topic.php', 'forum.php' => 'forums/forum.php' 'functions.php' => 'func/functions.php'); Hello PHPFreaks! I have a question for somebody to answer Code: [Select] <?php //This is the directory where images will be saved $target = "gallery/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $username=$_POST['username']; $photo=($_FILES['photo']['name']); $caption=$_POST['caption']; $ip=$_POST['ip']; //Writes the information to the database mysql_query("INSERT INTO `photo` (username, photo, caption, ip) VALUES ('$username', '$photo', '$caption', '$ip')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your caption has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> When pictures are uploaded; they upload the name they have named them. Is it possible to add to the script $username somewhere to the file name? For example; DSC_0077_SOLAR.JPG Thanks for your time! I m printing the prices of the items in my db. I want to print the prices like ; Price --- The format I want 75.000 --- 75 75.500 --- 75.5 100 --- 100 100.5 --- 100.5 1234.654 --- 1,234.654 1234.200 --- 1,234.2 1123456.789 --- 1,123,456.789 Simply I want to cancel the zeros after dot, and want to put comma every 3 digits before the dot. How can I do this ? Hello:
How could I use php to automatically insert the correct 3 letter month abbreviations and four digit year in the following pdf filenames on the first of each month?
(For example "...Feb2015.pdf" then "...Mar2015.pdf" and so on....
<div>Book One: <a href="http://monthlybookfi...One_Jan2015.pdf">Download Here</a></div> <div>Book Two: <a href="http://monthlybookfi...Two_Jan2015.pdf">Download Here</a></div> <div>Book Three: <a href="http://monthlybookfi...ree_Jan2015.pdf">Download Here</a> </div> <div>Book Four: <a href="http://monthlybookfi...our_Jan2015.pdf">Download Here</a> </div> Please advise, Eggie I am looking at using PHP (or whatever) to compress images/video/music when it is uploaded to be stored on the server and decompressed when required on the webpage. I know I will have to make a decompress function so it knows when to decompress to its full size, but any ideas on how to do the compression. Requirements: biggest, best and efficient compression of music/image/video files. Thanks in advance. I need to display multiple files that have a similar file name, but there are timestamps added to the filename once its been modified. What could I use in PHP to display all these files? Right now I use a $path variable that looks into a SQL table to build the path to the folder that the files reside in.. ELSEIF($Recordmyrow['Applicatio'] == "pdf file") { IF($securityvalue != "P"){$display = false;} IF(file_exists(substr($path, 5))){} ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";} } So there is a file called file1.pdf then another one called file1.mmddyyyy.pdf My code shows file1.pdf but not file1.mmddyyyy.pdf Is there some kind of wildcard I can use to get it to show all the files if they exist? Hey Guys, I want to be able to upload a large amount of mp3's to a private area on our website and have a script that will scan the folder for all the filenames and then create a record in the db of all the filenames. It is to help with creating the playlist for my friends radio show. To manually insert all the filenames each month woudl be a nightmare! I have been messing with a partial script I found on the web, and it kind of does something but not what I want it to. heres the db id int(4) title varchar(250) mp3 varchar(250) sort int(4) visible int(1) added datetime heres the script i am trying to use: <? $listDir = array(); $dir = "./music"; if($handler = opendir($dir)) { while (($sub = readdir($handler)) !== FALSE) { if ($sub != "." && $sub != ".." && $sub != "Thumb.db") { if(is_file($dir."/".$sub)) { $listDir[] = $sub; }elseif(is_dir($dir."/".$sub)){ $listDir[$sub] = $this->ReadFolderDirectory($dir."/".$sub); } } } closedir($handler); } $db = mysql_connect("localhost","gfab_ml","xxxxxxx"); mysql_select_db("gfab_ml",$db); $colors=serialize($listDir); //takes the data from a post $sql="INSERT INTO ml_music (ID, title, added) VALUES('','$colors','')"; $result = mysql_query($sql) or die (mysql_error()); ?> it does insert the values into the db but what it does is creates 1 record and spits all the results in a really weird way like this. into the title field. a:18:{i:0;s:23:"14 Mr Magic Premier.mp3";i:1;s:23:"track1.mp3";i:2;s:15:"track2.jpg";i:3;s:15:"track3.fxp";i:4;s:12:"track4.mp3";i:5;s:7:"track5.mp3";i:6;s:7:"track6.jpg";i:7;s:7:"track7.mp3";i:8;s:12:"track8.mp3" Could anyone please help me to make it work? im a bit of a noob. but i do like to tinker! Dear all, is there any library that supports text extraction from docx,doc, excel, pdf, etc formats like Apache POI does on Java? Or should I port Apache POI classes to PHP code? best regards, ethereal1m Hi guys. Just a quick PHP question. I have a script running that gets the files from a directory. There is then a switch function that decides what to do with the file, ie... if it is a PNG... DO THIS.. if it is a docx... DO THIS.. etc. And i want each file to be in a different cell on the table. The table can be longer wider than 5 columns (however it can be as high as it wants). I currently have this bit working... but instead of one item in each cell... it is displaying every item in each cell. Any ideas Chaps? Here is my code Many thanks in advance if you could possibly help.. echo "<table><tr>"; $count = count(glob("../../shared/files/*.*", GLOB_BRACE)); for($i=1; $i <= $count; $i++) { /// echo "<td>"; /// if ($handle = opendir('../../shared/files/')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $break = pathinfo($file); $b = $break['extension']; switch (@$break[extension]) { case 'bmp' : echo 'A BMP<br>'; break; case 'png' : echo 'A PNG<br>'; break; case 'doc' : case 'docx' : echo 'A DOC OR DOCX<br>'; break; default: echo 'FILE NOT RECOGNISED<br>'; } } } closedir($handle); } /// echo "</td>"; /// if($i % 3 == 0) echo "</tr><tr>"; } echo "</tr></table>"; ?> |