1

So I got this working locally but it's not working on my server. Is the problem due to me using the scp command to transfer my files up to the server?

Here's my code:

// Local
if ('localhost' == $_SERVER['HTTP_HOST']) {
    $cmd = 'find resume*.pdf -type f -print0 | xargs -0 stat -f "%m %N" |sort -rn | head -1 | cut -f2- -d" "';
// Production
} else {
    $cmd = 'find resume*.pdf -type f -print0 | xargs -0 ls -drt | tail -n 1';
}
$results = exec($cmd);
echo '$results = ' . $results;

Local Output:

$ php -f index.php
$ $results = resume june 2014.pdf

Remote Output:

$ php -f index.php
$ $results = resume_may_2014.pdf

Here's what it looks like when I look at the modification dates of the files on the server. I also can't figure out why they (as in the resume files with the same modification date) are getting ordered this way. It's not like they are sorted by alphabetical, file size, etc.

<!-- language: lang-bash -->
username@username.com [~/www/resume]# ls -lt
total 432
drwxr-xr-x  6 username username   4096 Jun  1 14:05 ./
-rw-r--r--  1 username username    927 Jun  1 14:00 index.php
-rw-r--r--  1 username username   2028 Jun  1 13:55 error_log
-rw-r--r--  1 username username 135855 Jun  1 13:37 resume_may_2014.pdf
-rw-r--r--  1 username username      0 Jun  1 13:37 resume_feb_2014.pdf
-rw-r--r--  1 username username 118698 Jun  1 13:37 resume\ june\ 2014.pdf
drwxr-xr-x  4 username username   4096 Jun  1 13:18 resume\ june\ 2014.pages/
-rw-r--r--  1 username username 135855 May 31 18:59 resume.pdf

Articles Referenced:

Community
  • 1
  • 1
Alex Cory
  • 10,635
  • 10
  • 52
  • 62
  • The file list is sorted by modification time (newest first) as you've requested with the `-t` option. – ooga Jun 01 '14 at 20:54
  • Thanks @ooga! Notice the part I added (as in the resume files with the same modification date) part. ie: how these files are sorted: resume_may_2014.pdf, resume_feb_2014.pdf, resume\ june\ 2014.pdf, etc. That is what I'm confused by. – Alex Cory Jun 01 '14 at 20:55
  • Sorry. I obviously didn't read your question very well. Within the groups of files with the same modification date they are probably in order of the inodes, which is not particularly useful. – ooga Jun 01 '14 at 21:00
  • Perhaps the creation time (`-c`) will be more useful to you than the last modification time. – ooga Jun 01 '14 at 21:10

1 Answers1

1

Why not do this in PHP?

<?php
$directory = '/path/to/directory';
$filename = null;
if ($handle = opendir($directory)) {
    while (false !== ($entry = readdir($handle))) {
        // if (!is_dir($entry)) { // no directories, only files. (Otherwise exclude directories . and ..)
        if (substr($entry,0,6) == 'resume' && substr($entry,-4) == '.pdf') { // only resume*.pdf
            if ($filename === null || $time < filectime($entry)) { // or use filemtime() for last modified time in stead of last change time
                $time = filectime($entry);
                $filename = $entry;
            }
        }
    }
    if ($filename === null) {
        echo "No files found";
    } else {
        echo "Last changed file is: " . $filename . ". It was last changed at: " . date('r', $time) . ".";
    }
    closedir($handle);
} else {
    echo "Could not open directory";
}
?>

DEMO

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • You're awesome right now. The reason I'm using bash is because I want to learn it more, but this totally works. Thanks so much! – Alex Cory Jun 01 '14 at 21:33