ballouta Posted May 25, 2008 Share Posted May 25, 2008 Hello I searched the site here for a PHP code that searches for a file in a directory. I didn't find what i exactly need (http://www.phpfreaks.com/forums/index.php/topic,184175.0.html) The code I need is that I want to show all files found in a specific folder, show the extension separated, then show each file creation date. e.g: File Name Extension Creation Date ======== ======= ========== fileone .mdl 5/25/2008 Many thanks Quote Link to comment Share on other sites More sharing options...
calabiyau Posted May 25, 2008 Share Posted May 25, 2008 Something like below should get you on the right path if ($handle = opendir('path/to/folder')) { while (false != ($file=readdir($handle))) { if ( ($file != ".") && ($file != "..") ) { $array = explode( '.' , $file ); //ABOVE ASSUMES THERE IS ONLY ONE FILE EXTENSION //WOULD NOT WORK FOR THINGS LIKE file.inc.php echo "FILENAME:" . $array[0]; echo "EXTENSION:". $array[1]; $created = filectime($file); echo "CREATED ON: " . date("l, dS F, Y @ h:ia", $created); //THERE ARE OTHER WAYS YOU CAN FORMAT THE DATE //IF YOU SEARCH AROUND A BIT } } } Quote Link to comment Share on other sites More sharing options...
ballouta Posted May 26, 2008 Author Share Posted May 26, 2008 The code is perfect, just had a samll problem i am getting an error on this line: $created = filectime($file); open this link to read the error: http://www.motif-services.com/date2.php when i comment this line, no errors appear: http://www.motif-services.com/date.php Thanks Quote Link to comment Share on other sites More sharing options...
skot Posted May 26, 2008 Share Posted May 26, 2008 I think with filectime() you have to also use the date() function to tell php how you want your date formatted. Try this: $created = date('l, dS F, Y @ h:ia', filectime($file)); Quote Link to comment Share on other sites More sharing options...
jamieh Posted May 26, 2008 Share Posted May 26, 2008 http://uk3.php.net/filectime Instead of putting filectime() into a variable try putting it directly in the code to see if this changes anything, that link does just that. I'm only a complete beginner at php being honest but just trying to help. Jamie Quote Link to comment Share on other sites More sharing options...
ballouta Posted May 26, 2008 Author Share Posted May 26, 2008 Hi Skot, i tried your code line, i got the same error: As for jamieh code, as shown in the example, I added that if (if (file_exists($file)) ) and it works, but doesn't show all files modification date!! very strange why? http://www.motif-services.com/date.php <?php if ($handle = opendir('malek')) { while (false != ($file=readdir($handle))) { if ( ($file != ".") && ($file != "..") ) { $array = explode( '.' , $file ); echo "FILENAME:" . $array[0]; echo "<br>"; echo "EXTENSION:". $array[1]; if (file_exists($file)) {echo "<br> was last changed: " . date("F d Y H:i:s.", filectime($file)); } echo "<br><br><br>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 26, 2008 Share Posted May 26, 2008 To get the filename and extension right every time, also when a filename contains more than one dot, use <?php $file = 'file.name.inc.php'; $info = pathinfo($file); // $info['extension'] holds 'php' // $info['filename'] holds 'file.name.inc' ?> Also note that filectime() returns the last modified date. There's no function for the creation date, since "there is no creation time for Unix files in most Unix filesystems". Try filemtime() instead of filectime, and see if that works better. EDIT: And you shouldn't check if the file exists, since it always will. Remember you're scanning the directory for files. Quote Link to comment Share on other sites More sharing options...
ballouta Posted May 26, 2008 Author Share Posted May 26, 2008 hi thebadbad your right, no need to check if the file exists or not. As for replacing filectime () with filemtime(), I got the same error But your code is better and short, even though i still have a problem. I wrote: <?php if ($handle = opendir('malek')) { while (false != ($file=readdir($handle))) { $info = pathinfo($file); echo "$info['extension']"; echo "$info['filename']"; } } ?> I got this error: http://www.motif-services.com/date2.php Kindly fix it for me. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 26, 2008 Share Posted May 26, 2008 Got a better solution to slim down the code, use glob(). Whole code (tested, works here without errors): <?php $dir = 'malek'; $files = glob("$dir/*"); if ($files) { foreach ($files as $file) { $info = pathinfo($file); echo 'Filename: ', $info['filename'], '<br />Extension: ', $info['extension'], '<br />Last modified: ', date('F d Y H:i:s', filemtime($file)), "<br /><br />\n"; } } else { echo 'Error: No files were found.'; } ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 26, 2008 Share Posted May 26, 2008 But to correct your error, use curly brackets, or simply remove the double quotes: <?php if ($handle = opendir('malek')) { while (false != ($file=readdir($handle))) { $info = pathinfo($file); echo $info['extension']; echo $info['filename']; } } ?> Quote Link to comment Share on other sites More sharing options...
ballouta Posted May 26, 2008 Author Share Posted May 26, 2008 Thank you it is working Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 26, 2008 Share Posted May 26, 2008 And I know why you got those errors - in your method $file contained only the filename + extension; in my method $file contains the whole path "malek/filename.extension", which should be used when checking for the last modified date. Quote Link to comment 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.