0

I'm trying to build a solution for a project that will involve periodic file uploads from a camera to a web server, and then have the web server display the most recent images and run periodic cleanup. The camera's functionality is very limited; I can't have it delete files when it uploads new files, rename old files, or anything like that. The most I can do is append timestamps to a set prefix. Therefore, I'm trying to solve this issue from the server side.

Looking at the DirectoryIndex directive, I can't see a way that I could serve the most recent file as the default document, and I couldn't find any other directives in the Apache documentation that look like they'd have any impact. The end goal is to use this in a widget on a monitoring dashboard, so using a static page or a directory index page using IndexOrderDefault wouldn't work either. Would it be possible to do this using just Apache, either by looking at the alphabetically last file (most recent timestamp in the file name) or by looking at the most recently created file?

2
  • You might want to rethink the approach.... Have a static page that simply has a list of links. The links can be updated fairly easily, by writing the file name into the static page.
    – Drifter104
    Sep 1, 2016 at 15:44
  • @Drifter104: If it comes down to it and there's no other way, I'm already planning on writing a PHP-based solution to handle this use case dynamically. I've edited the question to explain why a static index page using (for example) IndexOrderDefault isn't an option. I'm just trying to figure out if there's a way to do this without resorting to dynamic pages.
    – aletson
    Sep 1, 2016 at 15:50

3 Answers 3

0

If you have PHP installed, you could get that to fetch the contents of the most recent file in the folder by placing this in the index.php file.

$directory = dirname (__FILE__);
$contents = scandir ($directory);

$recent_file = null;
$modified = 0;
foreach ($contents as $filename)
{
    if (is_file ($filename) && filemtime ($filename) > $modified)
    {
        $modified = filemtime ($filename);
        $recent_file = $filename;
    }
}

echo file_get_contents ($recent_file);

Some notes: I haven't bothered to do a check for a null filename since it will always be able to find the index.php file. So you might want to put in an exception in the if statement ($filename != 'index.php) and add in a possibility of $recent_file having a null value and dealing with that as you wish.

Note that this does not allow for parsing of the returned file. This would only work if you were serving static HTML files. Another note - this is not binary safe! You should add a HTTP header with an appropriate MIME-type if you wish to dish out binary files (e.g. most recent image from webcam snaps?).

1
  • This was the best answer for my situation, because it didn't involve any file level operations. Running a cron job was, for me, not as good an option as it could have led to outages if, for example, a file had not been completely uploaded yet. I used PHP's DirectoryIterator with isFile() to test for existence and getExtension() to validate the extension, and used a comparison of mtime to get the most recently modified file that was not currently being modified (avoiding file uploads in progress). Thanks for your help!
    – aletson
    Sep 1, 2016 at 17:58
0

Yes, You can create a SHELL script which will run on some time interval. And that script will find the last updated file and replace the DirecrtoryIndex in the .htaccess

The script will be something like the below:

#!/bin/bash

LASTFILE=$(ls -ltr | tail -n 1 | awk '{print $9}')

sed -i "s/$DirectoryIndex.*/DirectoryIndex\ $LASTFILE/g" .htaccess

0

Similar to @Sourav Maity 's answer, you could also use a Shell script to rename the most-recent file to index.html (or whatever Apache is set up to serve).

find /path/to/search -type f -printf ‘%TY-%Tm-%Td %TT %p\n’ | sort -r | head -n1 | xargs -I '{}' mv '{}' index.html

then have cron run the script as often as you need.

4
  • Thanks! This is probably what I would go with if I end up implementing a cron based solution because it means I wouldn't have to use a .htaccess file. My only concern is that a cron job, by definition, would be scheduled - and my goal is to have the new image show up immediately on upload.
    – aletson
    Sep 1, 2016 at 16:07
  • You could run the cron job every minute - not quite immediate, but close. Also, take a look at the inotifywait package. I haven't used it, but it looks like a possibility Sep 1, 2016 at 16:12
  • Use a symlink rather than rename. Sep 1, 2016 at 16:26
  • @RogerCreasy: I looked into inotifywait, and it would have probably worked as a solution, but I decided PHP better fit my needs for this project as that way I wouldn't have to worry about making changes to files on the server. Appreciate your answer - +1!
    – aletson
    Sep 1, 2016 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .