NotionCommotion Posted July 19, 2016 Share Posted July 19, 2016 I have some files stored in a directory and not a database, and I display the date uploaded as date ("F d Y H:i:s.", filemtime ('/path/to/file.ext')), and do something similar using filesize() for size. I have some other files stored in a directory and their name and surrogate key is also stored in a DB. Do you think I should also store the date uploaded/modified and size in the database instead of deriving it from the file? How should the date be stored so that it will act the same (i.e. daylight savings time, etc) as the first mentioned files where date is based on the Linux operating system? PS. The only reason I am storing the later files in a database is they are linked to another entities using a many-to-many table. Using the other entities PK and the filename instead of a surrogate file PK for the many-to-many table is a bad idea, right? Quote Link to comment https://forums.phpfreaks.com/topic/301513-storing-file-modification-date-in-database/ Share on other sites More sharing options...
kicken Posted July 19, 2016 Share Posted July 19, 2016 If you want to ensure the date doesn't accidentally change then storing it in the database when you create the file would probably be the best thing. If you ever decide to move the storage location of the files then the filesystem date may end up changing to the current date/time for all the files making it look like you uploaded everything all at the same time. As far as how to store the date, just use a DATETIME column and store the time using the UTC timezone. When you read it back, do so as UTC then you can convert it to a local time if desired. //Assuming PHP is configured to use the local timezone by default. echo 'Local Time: ', (new DateTime())->format('Y-m-d H:i:s'), PHP_EOL; $dt = new DateTime(); $dt->setTimezone(new \DateTimeZone('UTC')); $store = $dt->format('Y-m-d H:i:s'); echo 'UTC Time: ', $store, PHP_EOL; $dt = new DateTime($store, new \DateTimeZone('UTC')); $dt->setTimezone(new \DateTimeZone(date_default_timezone_get())); echo 'Restored local time: ', $dt->format('Y-m-d H:i:s'), PHP_EOL; Quote Link to comment https://forums.phpfreaks.com/topic/301513-storing-file-modification-date-in-database/#findComment-1534664 Share on other sites More sharing options...
requinix Posted July 20, 2016 Share Posted July 20, 2016 To me it depends on what the date represents. If it's an actual piece of relevant data then you should put it in the database, but if it's just an artifact of the file itself then leave it with the file. So probably the former. Quote Link to comment https://forums.phpfreaks.com/topic/301513-storing-file-modification-date-in-database/#findComment-1534679 Share on other sites More sharing options...
NotionCommotion Posted July 20, 2016 Author Share Posted July 20, 2016 Thanks kicken and requinix, Good points about dependency on the filesystem and whether it is a artifact. Not sure how to respond, and would like to provide more info. I am building a simple webpage builder which creates a page such as the below. The application allows the user to upload JS, CSS, and image files to directories /js/, /css/, and /images/ respectively, and this is the part where the date uploaded and file size is presented to the administrator user. The application also allows the user to input desired HTML for a given page which will be presented in #content, and to link JS and CSS resource files to that page which might be locally hosted (as described above) or which are externally hosted. Image files are only linked to the page via the pages HTML markup. So, I have table "pages" - id (pk, int) - name (varchar) - HTML (text) My dilemma is how do I link the local and external resources to the page? Do I create a table which includes the page ID and the resource path such as '/js/file_1.js' or 'http://bla.com/external_file_1.js'? Or do I attempt to normalize the local files and include their path in a separate file and deal with externally located resources differently? <!DOCTYPE html> <html> <head> <title>Page #1</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type='text/javascript' src='/js/file_1.js'></script> <script type='text/javascript' src='/js/file_2.js'></script> <script type='text/javascript' src='http://bla.com/external_file_1.js'></script> <link rel="stylesheet" type="text/css" href="/css/file_1.css" /> <link rel="stylesheet" type="text/css" href="/css/file_2.css" /> <link rel="stylesheet" type="text/css" href="http://bla.com/external_file_1.css" /> </head> <body> Some global HTML goes here... <div id="content"> Possible user provided HTML goes here... <img src='/images/image_1.jpg' alt="x" /> Possible user provided HTML goes here... <img src='/images/image_2.png' alt="x" /> Possible user provided HTML goes here... <img src='/images/image_1.jpg' alt="x" /> Possible user provided HTML goes here... <img src='/images/image_3.jpg' alt="x" /> Possible user provided HTML goes here... </div> Some more global HTML goes here... </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/301513-storing-file-modification-date-in-database/#findComment-1534686 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.