PHP - How To Get Most Recent File From Directory & Sub-directories
I have used the following script to correctly display the first file from the array across the selected directory and its sub-directories. Does anyone know how to modify this code to only echo the most recent file based on filemtime across the directory/subdirectories?
Code: [Select] function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } echo array_shift(ListFiles('media/media/images')); Similar TutorialsHello I have a problem. I have been trying to copy my music folders structure but not copying the mp3s; instead using the file names as a file name for a .txt file and inserting the URL to the file in the .txt file. Eg. E:\My Music folder 1 folder 1.1 file.mp3 folder 2 folder 3 to G:\My Music folder 1 folder 1.1 file.txt folder 2 folder 3 and inside "file.txt" would be one line saying "http://www.xxx.com/my_music/file.mp3 i am getting this as a echo, Code: [Select] Directory handle: Resource id #2 Files: . ..why? Code: [Select] <? if ($handle = opendir('images')) { echo "Directory handle: $handle\n"; echo "Files:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))){ if (is_dir($file)){ echo "$file\n"; } } closedir($handle); } ?> I must be brain dead...I've searched everywhere and can't seem to find a snippet for this. I'm just looking for some code to show a directory listing, but only certain files (by extension) and I don't want to show any sub-directories. I thought I might have been able to do this in the .htaccess file, but I can't find anything that will get rid of the sub-directories. So, I figure there must be a way to get a file listing, check the extension, add it to the page, or skip it if it's a sub-directory or doesn't match the extension. Or, I could just be totally, way off...anyway, any help would be greatly appreciated...I'm fairly new to php. How do I include a file that is located two directories above the file that I want to include the file into? My absolute hosting path is /home/content/52/8840652/html if this would be of any use. I'm not quite sure about the PHP include functions. Any help would be greatly appreciated, Thanks! I have created a table called directories. Now basically I have two fields; dirID (INT) directory (VARCHAR) now I am fully aware this might sound like more of an SQL question but I have three physical directories: db/images/web1 db/images/web2 db/images/web3 I have inputted them into the database but I was just wondering, how could I created a page which links those records to a directory. I want to have each row hyperlinked, with the output page displaying list of contents for each directory. How can I possibly achieve this please? I have a file upload script that will eventually process a ton of files. I would like to upload them into sub-directories according to what year, month, and day they are uploaded.
A typical tree should look like this:
attachments/
--/2014
-----/January
--------/01
--------/02
--------/03 , etc.
-----/February
--------/01
--------/04
--------/09
--------/18
--------/20, etc
-----/March, etc
--/2015, etc.
So a file called image.jpg uploaded on 10/31/2014 would have a URL of attachments/2014/October/31/image.jpg. I understand that every time a file is uploaded, the script would have to detect through FTP whether or not folders for the year, month, and day exist, and if they don't create them. My problem is that I have no idea what the logic of this script would be. What order should I do things in? Is there a way to use maybe foreach to detect/create the folders? Any input would be appreciated.
I havent included the whole title as it wouldnt let me but I was wondering if someone could help me on this? I know this is possible as torrentflux caters for this but unsure of where to start. I dont want to allow file or directory uploads or creation in my /etc/php.ini file (this is turned off). Yet then torrentflux allows me to link a torrent from an external source (using legal downloads of course ) but then it uploads it on my server and creates folders on a per user basis. How is this possible can someone give me some pointers please? I look forward to any replies, Jeremy. I'm in the middle of making my own CMS system, where a user can create a page online via a text editor, I know how to create and save the file, but to make the process easier I would like to a function where a user can click a button find a list of files and folders, pick the folder location to save a new file to. Instead of having to type something like this /commonPages/aboutUs/ with the file name being test.php What would be the easiest way to do this? I have just been looking at opendir() and my first thought was to use <input type="file" />, but then I remember that was only for inputting a particular file name, which would be useful for renaming a file at a later date. For example, if we wanted to change test.php to something more meaningful ie history.php In my script, I'm trying to create a file with the name of a variable in a different directory. My code is: Code: [Select] $handle = fopen("cheese/" . $page . ".html", "w");Output: Warning: fopen(cheese/test.html) [function.fopen]: failed to open stream: No such file or directory in /home/a5938041/public_html/newfile/newpage.php on line 6 What's happening here? (sorry I'm a total noob.) Why does this code: Code: [Select] require_once "../../media/php/classes/Mysql.inc.php"; Work when I go to: http://mysite.com/API/v1/index.php But when I go to: http://mysite.com/index.php it doesn't work. The require is being called from another class, which I call from the two pages above. The class loads, but in the second link, I get failed to open stream: No such file or directory but the first one works fine. What would cause this? could this be an htaccess thing? htaccess: Code: [Select] Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+)$ /$1.php [L,QSA] $file="../myclients/clients/".$company_name."/Download/".$file_name; $size = filesize($path); // not working i try with function also function formatbytes($file, $type) { switch($type){ case "KB": $filesize = filesize($file) * .0009765625; // bytes to KB break; case "MB": $filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB break; case "GB": $filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB break; } if($filesize <= 0){ return $filesize = 'unknown file size';} else{return round($filesize, 2).' '.$type;} } echo formatbytes($file, "KB"); uploaded file size is 488 kb but it showing "unknown file size" please help me. Hey, I dont know whats wrong but i was hoping someone could take a look at the code and provide some errors that maybe causing this script not to work. I want it to display all the .jpg pictures in thumbnails, it currently does not even get the pictures. code: <?php $pic_listing = mysql_query("SELECT * FROM toplist WHERE id='". $id ."'") or die(mysql_error()); while($serverpic = mysql_fetch_array($pic_listing)) { $servername = $serverpic["servername"]; } $directory = "images/serverpic/" . $servername . ""; $images = glob("" . $directory . "*.jpg"); if(empty($images)) { ?> <div class="highslide-gallery"> <?php foreach($images as $image) { ?> <a href="images/serverpic/<?php echo $servername . "/" . $image; ?>" class="highslide" onclick="return hs.expand(this)"> <img src="images/serverpic/<?php echo $servername . "/" . $image; ?>" alt="Highslide JS" width="100" height="100" title="Click to enlarge" /> </a> <div class="highslide-caption"> <?php } ?> </div> </div> <?php } ?> After a user on my site deletes their account I want their profile picture to be deleted from my images folder. I was testing the unlink function on my index.php file and it is failing. Here is the error: Warning: unlink(/assets/img/avatars/users/jasongordon.png): No such file or directory in /home/www-data/big.com/index.php on line 86 My code was just: unlink('/assets/img/avatars/users/jasongordon.png'); I'm not sure why this fails because my file does exist. file_get_contents() has suddenly stopped working for me. I keep getting this error: Code: [Select] Warning: file_get_contents(/Users/ttocskcaj/MineCMS/public/js/jquery.js ) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in /Users/ttocskcaj/MineCMS/controllers/m.controller.php on line 44 The file(s) definitely exist, I can use nano to open them, and permissions are 777 on all the files. The lampp error log has nothing. Not even a No such file or directory error as you'd expect. Any ideas what's going on? I have a file system browser i'm writing for a client. I would like to know if there's a way to determine if an object is a directory or file, so I can display a proper icon next to it depending on what it is. Any help is appreciated. hey guys im tyring to read my directory (whole of the web server)...i want to be able to put the file name and root into an array...im just wanting to know whats the best function i need to do this please?...thank you Hi,
I have this code
<?php // Must include this include("geoiploc.php"); function getYJLINKS($default_font_family,$yj_copyrightear,$yj_templatename,$show_tools,$show_fres,$show_rtlc,$validators_off,$totop_off){ $allLinks = array(); if((function_exists('toolbox_urls') && $show_tools == 1) || $validators_off == 1 || $totop_off == 1){ echo '<div class="validators">'; if($validators_off == 1){ $allLinks[] ='<a href="http://jigsaw.w3.org/css-validator/check/referer?profile=css3" target="_blank" title="CSS Validity">CSS Valid</a>'; $allLinks[] ='<a href="http://validator.w3.org/check/referer" target="_blank" title="XHTML Validity">XHTML Valid</a>'; } if($totop_off == 1){ $allLinks[] ='<a class="yjscroll" href="#stylef'.$default_font_family.'">Top</a>'; } if (function_exists('toolbox_urls') && $show_tools == 1): global $font_size; global $font_direction; if ($show_fres == 1): $allLinks[] = '<a id="fontSizePlus" class="fs" href="javascript:;" rel="nofollow">+</a>'; $allLinks[] = '<a id="fontSizeMinus" class="fs" href="javascript:;" rel="nofollow">-</a>'; $allLinks[] = '<a id="fontSizeReset" class="fs" href="javascript:;" rel="nofollow">reset</a>'; endif; if ($show_rtlc == 1): $allLinks[] = '<a class="tdir" href="'.$font_direction[1].'" rel="nofollow">RTL</a>'; $allLinks[] = '<a class="tdir" href="'.$font_direction[2].'" rel="nofollow">LTR</a>'; endif; endif; echo implode(' | ',$allLinks); echo '</div>'; } // loading this from a database $ip = $_SERVER["REMOTE_ADDR"]; // listing Country Flags Images $dir = 'images/flags'; $file_display = array('png'); if (file_exists ($dir) == false) { echo 'Directory \'', $dir, '\' not found!'; } else{ $dir_contents = scandir($dir); foreach($dir_contents as $file) { $file_type = strtolower(end(explode('.', $file))); If($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) if($file == '..' || $file == '.') { continue; } $code = strtolower(getCountryFromIP($ip, "code")); echo '<div class="yjsgcp">Palana.it ® 1994 - '.$yj_copyrightear.' :: Tutti i diritti riservati :: Realizzazione Palana.it ││ Utente rilevato: IP: '. $ip . ', '.getCountryFromIP($ip, " NamE ").' ('.getCountryFromIP($ip, "code").'-'.getCountryFromIP($ip, "AbBr").') '; echo '<img src="', $dir, '/', $code, '.png" /> </div>'; } } } ?>The problem, as we see (http://www.palana.it), is that code printed the Italian flag but many times there are images in the directory, that is over 204. See bottom of the page. How can I fix the code to get ONE display? Hello, I've got what should be a simple solution here. I've tried many things but it just won't work for me. This is what I have. I've got a wysiwyg editor setup to write to a file called news.php. The file that has the editor is edit/index.php. It's just a simple textarea. The textarea is supposed to include news.php. For some reason, it won't let me include that file that is in a lower directory. The file that writes to news.php is called edit/write.php. edit/write.php doesn't write to news.php instead it writes to edit/news.php. The only reason I want to include the lower directory is so that I can put a .htaccess password protection on the edit directory. I'll include some code below. Code: (edit/index.php) [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr"> <head> <title>Editing News</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <script type="text/javascript" src="nicEdit.js"></script> <script type="text/javascript"> bkLib.onDomLoaded(function() { nicEditors.allTextAreas() }); </script> <form method="post" action="write.php"> <p> <textarea name="content" style="width:100%;height:1000px;"><?php include '../news.php';?></textarea> <input type="submit" value="Save" /> </p> </form> </body> </html> Code: (edit/write.php) [Select] <?php $File = "../news.php"; $Handle = fopen($File, 'w'); $Data = $_POST['content']; fwrite($Handle, $Data); fclose($Handle); header("Location: index.php"); ?> index.php is just a simple include news.php. Am I missing an easy step? Any help would be great!! Thanks! After the file gets uploaded, I intend the file to become renamed, and then moved to its destination folder. Though to move the file, I need the newly renamed file name and also its location, which is the temporary folder, and the latter describes my question. This is the script as follows: Code: [Select] $avatar_tmp = $_FILES['avatar_upload']['tmp_name']; // Rename the file into a more usable file name $new_file_name = $user_name . '_' . rand(111111, 999999) . '.jpg'; rename($avatar_tmp, $new_file_name); // Move the uploaded file on the disk to its folder // The directory of the temporary folder is needed in front of the new_file_name variable move_uploaded_file ($new_file_name, $target); The directory of the temporary folder is needed in front of the new_file_name variable and my question is, is there any function, to get the directory of the temporary folder for the uploaded file? So I can insert it in front of the new_file_name variable and move it out of the folder to its destination folder? I'm trying to work out if in my form my image field, which currently asks the user to upload from their hard drive, can show the contents of a server folder to select an image or upload a new one. Can this be done?? |