PHP - List Files In A Directory And Grab The First Filename
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!! Similar TutorialsI 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. 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? 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); ?> Hey guys! I'm making a locally run web based application for entering/storing/editing data. I am trying to do this thing where I export a 'records.txt' file to a different location which can be selected by the user. I have a lame <input type='text' value='H:\'> box going on which I was going to use to define the path for the file to be saved, but I want to be able to use a pop up windows (or whatever native OS finder application) just like the one which appears when you use <input type='file'>. Now that I think about it, I think this is more of a html question, but it's 2.30am and my project is due today... See! what the hell, I just habitually pressed ctrl-s after working in dreamweaver for hours and the exact function which I am looking for popped up, how do I do that?! Please thanks guys I am trying to display images on a html page alphabetically in descending order by their filename. <?php $dir = "./plate_cache"; $handle = opendir($dir); while (($file = readdir($handle))!==false) { if (strpos($file, '.png',1)) { echo "<img id=\"plates\" src=\"/tags/plate_cache/$file\"></a><br />;"; } } closedir($handle); } ?> Any help is greatly appreciated! how do i grab and print on page in date order all .jpg files in a folder ? i have made an delete files script which works for only one directory but not sub directory so i want to delete files of same extention from directory and subdirectory. My current code is Code: [Select] <? $dir = 'hmm/'; function scanr($dir){ $arr = glob($dir.'/*.jpg'); foreach($arr as $vv){ //check if $vv is a file if(is_file($vv)){ //if file, get the filename $vx=explode('/',$vv); $file=$vx[count($vx)-1]; // if no extension delete the file unlink($vv); // print the deletion message echo $vv." deleted!<br>";}else{ // if $vv is a dir then scan it again for files scanr($vv); }} } scanr($dir); ?> 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 I almost have it but can't figure why the code below only shows one file per folder. I think it is showing the last file. I need $ul somehow looping to show all the files in $file. <?php $year = date('Y'); if ($handle = opendir("results/$year/Boys")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if ($handle2 = opendir("results/$year/Boys/$file")) { while (false !== ($file2 = readdir($handle2))) { if ($file2 != "." && $file2 != "..") { $ul = "<li><a href=\"#\">$file2</a></li>"; $li = "<li><a target=\"_blank\" href=\"javascript:void(0)\">$file</a> <ul> $ul </ul> </li>"; } // close if file2 }echo $li; } } } }closedir($handle);closedir($handle2); ?> Hi guys, I am using a MySQL table to store if a user has access permisisons to a file. The files are stored outside the webdirectory in drive F:\ So I have this code I have been playing around with in order to display the file I access and check permissions for: <?php $file = $_GET['file']; $myfile = 'F:\files\/'.$file.'.jpg'; echo "<img src='$myfile' />"; ?> So a sample case would be say on index.php <img src="http://somedomain.com/files?file=abcdefghijkl" /> and only if the current user had permissions to view that file would they see it. Any help on how I can do this is greatly appreciated. I do understand all my above code is probably totally wrong. Cheers I have a function that reads all the picture files in a folder and displays them as thumbnails. It also displays a large version of the first image in the folder and clicking the thumbnail changes the large picture. I'm trying to use pictures of arrows as a button to allow users to click and have the next picture displayed large rather than having to click each individual thumbnail but don't really know how to get started. Can anyone suggest a method or give me a hint? This is my code so far if that helps: function displayPhotos(){ global $columns,$dir,$album; generateThumbnails(); $act = 0; $keyid = 0; $thumb_selected = (isset($_GET['thumb']) ? $_GET['thumb']:''); if ($thumb_selected !==""){ $dirName = substr($thumb_selected,0,strpos($thumb_selected,basename($thumb_selected))); $thumbName = basename($thumb_selected); $thumbFile = $dirName.$thumbName; $large = str_replace('_th.jpg','.jpg',$thumbFile); } else{ $picture_array = glob("$dir*"); $large = $picture_array[$keyid]; } echo "<tr><td colspan='3' height='300' width='400' align='center'><img src='$large' alt=''></td></tr> <tr><td><img src='gallery/icons/arrow-blue-rounded-left.jpg' alt='Previous'></td><td></td><td align='right'><img src='gallery/icons/arrow-blue-rounded-right.jpg' alt='Next'> </td></tr><tr><td height='50'></td></tr>"; if ($handle = opendir("$dir")) { while (false !== ($file = readdir($handle))) { $file=$dir.$file; if (is_file($file)){ if (strpos($file,'_th.jpg')){ ++$act; if ($act > $columns) { echo "</tr><tr><td width='160' height='120' align='center'><a href='?album=$album&thumb=$file'><img src='".$file."' alt='".$file."'/></a></td>"; $act = 1; } else { echo "<td width='160' height='120' align='center'><a href='?album=$album&thumb=$file'><img src='".$file."' alt='".$file."'/></a></td>"; } } } } } } Thanks very much to anyone having a look at this! I am using the following code to list mp3 files from a directory and find out if there is a file for it in mysql if so not to include it in the list. The code below is listing each file twice I only would like them to be listed once. if($_POST['submit'] != 'select') { echo'Choose an mp3 to add information.<p><select name="file">'; $dir = "/uploads/"; $result = mysql_query("SELECT * FROM dj_pool") or die(mysql_error()); $num_rows = mysql_num_rows($result); // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $info = explode(".", $file); if($info[1] == "mp3") { $i = 0; while ($i < $num_rows) { $file2 = mysql_result($result,$i,mp3); if ($file != $file2) { echo "<option value='$info[0]'>$info[0]</option>"; } $i++; } } }closedir($dh);} } print "</select><p><input type=submit value=select name=submit>"; Hey guys, I've written a script that is suppose to Display the files within a specified directory /documents/ with check boxes to the right of them.. This worked out great. Then I wanted to devise a method to remove or unlink the selected files (via check box) when the user pressed a Submit button.. File Permissions on the server are 777 and I have hardcoded an unlink to test to see if it works and it does.. So the bug lies within my code.. Before I post the code let me explain my method in doing this. I created a for loop to name each check box after the corresponding file to the left and created a variable with the file name (to use later) Then on button press I made a for loop (it goes up to 50 because I wasn't sure how to make it the exact number of files and I didn't want to overload the server with say 1000000 :?) that goes through and checks the check box's values and if they equal 1, unlink the file in /documents/ Here's the code: Code: [Select] <html> <body> <form action="" method="post" class="delete"> <?php $dir = "documents/"; if ($handle = opendir($dir)) { echo '<h2>List of Files in '.$dir.'</h2>'; echo '<table>'; for ($i = -2; false !== ($file = readdir($handle)); $i++) { if ($file != "." && $file != "..") { echo '<tr><td><a href="'.$dir.''.$file.'">'.$file.'</a></td><td><input type="checkbox" name="Del['.$i.']" /></td</tr>'; $DelFile[$i] = $file; } } echo '</table>'; closedir($handle); } ?> <br /> <input type="submit" name="submit" value="Delete Checked Files" /> </form> <?php for ($i = 0; $i <= 50; $i++) if ('Del['.$i.']' == 1) unlink("'.$dir.''.$DelFile[$i].'"); ?> </body> </html> Help is extremely appreciated, and Thanks in advance guys! I have a directory full of pictures that I would like to display in a gallery. Originally I was going to write HTML like this...
<li> <img src=/photos/img_001.jpg"> </li>
And then simply copy and paste that for the number of photos I have, and then tweak the code to: img_002.jpg, img_003.jpg, and so on. I guess that is silly considering that I have over 500 photos to display! How could I use PHP to go to my /photos/ directory, scan all of the files in that directory, and then grab the file name so I could automate this process? Sorry, but I haven't written PHP in several years so all of this escapes me! ☹️ Hello everyone. I am having trouble trying to write some script that will unlink all files in a directory. Can anyone help me? I am not sure of the best way to do this? Thankyou. Dear sirs, I need to upload files to FTP and To waste less time, I zip the files, but the problem that since it is Automation it zips with directory - like this C:/DIR.zip in zip file I have a DIR/files.... How to unzip all files to main directory on the server. Right now if the main dir is /main/html/ it unzips to /main/html/dir/files But I need /main/html/files Thank you. Hello, Its my first day coding so please forgive me for some of the silly errors I might make.
I am writing a small upload script and I want to have some sort of orginazation by uploading images to a direcotry based on the day. The following is my code but it will fail to save the file Warning: file_put_contents(images/14-09-26/5425905a18bba.png): failed to open stream: No such file or directory This is becuase the directory with the data part is not created how should I go about first checking that this directory is there and then creating it ? <?php ini_set('display_errors',1); error_reporting(E_ALL); $today = date('y-m-j'); define('UPLOAD_DIR', 'images/' .$today. '/'); $img = $_POST['data']; $img = str_replace('data:image/png;base64,', '', $img); $img = str_replace(' ', '+', $img); $data = base64_decode($img); $file = UPLOAD_DIR . uniqid() . '.png'; $success = file_put_contents($file, $data); print $success ? $file : 'Unable to save the file.'; ?> Displaying the number of files in a directory and its subdirectorys <?php $path = './images_cache'; if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $fName = $file; $file = $path.'/'.$file; if(is_file($file)) $numfile++; if(is_dir($file)) $numdir++; }; }; closedir($handle); }; echo $numfiles.' files in a directory'; echo $numdir.' subdirectory in directory'; ?> How do you include files from a higher up directory? I'm currently working on a file in public_html/Directory/otherdirectory and want to include a config file that's in public_html/Directory so how would I include public_html/Directory/config.php in the public_html_Directory/otherdirectory/index.php file? I've tried using ../ and ../Directory/ in the includes line but got errors both times Edited April 11, 2020 by Nematode128Hi All I am trying to set up a system that where a user sets up an account the system automatically sets up a new directory and inserts template files into it. I understand mkdir is something to do with it. Does anyone know how to go about this please? thanks |