evilgenius82 Posted July 13, 2012 Share Posted July 13, 2012 Hey everyone, Im new here & also quite new to PHP. I am currently creating a project which reads files from a directory & lists the file names and certain file attributes. I have done quite well so far (in my opinion) but have come across a small issue. I have created a folder in the web directory & want to read files from it. Unfortunately the script is not reading files from it & is still reading files from root. I cant seem to fix at after playing around with the code for ages now. Any help would be much appreciated! <? $dir_name = './file_pool/'; //name of directory path. $iterator = new DirectoryIterator(dirname($dir_name)); //Instance DI foreach ($iterator as $fileinfo) { $link =( $fileinfo->getPathname()); //store path name as variable if (!$fileinfo->isDot()) { //Removes the . & .. if (!$fileinfo->isDir()) { //Removes other directories echo '<tr>'; echo '<td>', $fileinfo->getBasename() . '</td> <td>', $fileinfo->getExtension() . '</td> <td>', $fileinfo->getSize() . '</td> <td>', date("F d Y ", filemtime($fileinfo)). '</td> <td>', '<a href=" ', $dir_name . $fileinfo ,' ">Download <a/> ' . '</td> <td>',hash_file('md5', $fileinfo). '</td>' ; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265628-folder-issue/ Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 Don't use dirname(). You should be able to just use the folder name as the parameter. $iterator = new DirectoryIterator('file_pool'); If that gives you problems, try realpath : $iterator = new DirectoryIterator(realpath('file_pool')); Quote Link to comment https://forums.phpfreaks.com/topic/265628-folder-issue/#findComment-1361344 Share on other sites More sharing options...
evilgenius82 Posted July 13, 2012 Author Share Posted July 13, 2012 Thank you Scootstah! That solved it. Another issue which popped up is files such as .mp3, jpg & .psd are giving me warnings as shown in the image below. Other files such as .php and .css are fine. Thanks again for your fast response. Quote Link to comment https://forums.phpfreaks.com/topic/265628-folder-issue/#findComment-1361351 Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 Try using the absolute path to the file for filemtime() and hash_file(). Quote Link to comment https://forums.phpfreaks.com/topic/265628-folder-issue/#findComment-1361357 Share on other sites More sharing options...
evilgenius82 Posted July 13, 2012 Author Share Posted July 13, 2012 Hi everyone, I managed to fix it! Below is the code: $iterator = new DirectoryIterator($dir_name); //Instance DI foreach ($iterator as $fileinfo) { $link =( $fileinfo->getPathname()); //store path name as variable if (!$fileinfo->isDot()) { //Removes the . & .. if (!$fileinfo->isDir()) { //Removes other directories echo '<tr>'; echo '<td>', $fileinfo->getBasename() . '</td> <td>', $fileinfo->getExtension() . '</td> <td>', $fileinfo->getSize() . '</td> <td>', date("F d Y ", filemtime($link)). '</td> <td>', '<a href=" ', $dir_name , '/' , $fileinfo , ' ">Download <a/> ' . '</td> <td>',hash_file('md5', $link). '</td>' ; Quote Link to comment https://forums.phpfreaks.com/topic/265628-folder-issue/#findComment-1361367 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.