I am building a cron job that scans a directory of images and resizes anything over 800 pixels wide. I also only want it to enter into the resize if the image was uploaded that day so I tried to use filemtime() but I keep getting 1969-12-31 back.
I have read every forum post I could find on the issue and nothing seems to work. I have tested it on my local WIN 10 machine (using xammplite) and on my production Linux machine. Both are using PHP 5.3 or better and I get the exact same result.
Here's a code snippet along with echos I have added to see what's going on:
$files = scandir($_SERVER['DOCUMENT_ROOT'] . '/forum_functions/image_uploads');
$check_date = date('Y-m-d', strtotime('now'));
echo 'check date is ' . $check_date . '<br>';
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
echo 'file date is ' . date('Y-m-d', strtotime(filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}"))) . '<br>';
echo 'file is ' . $_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}" . '<br>';
echo 'file exists ' . file_exists($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}") . '<br>';
if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}") && $check_date == date('Y-m-d', strtotime(filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}")))) {
// do resizing
}
}
}
The first echo in the foreach loop prints 1969-12-31 for all the images
The second echo in the foreach correctly prints the path
The third echo returns true for file exists but it fails to enter the if condition because of the date being wrong.
I have checked for warnings in the logs and there are none, I have checked folder permissions and they are all fine.
I just done get it! Maybe my eyes are missing something or there's something happening I am not privy to so I could really use some advice.