0

This is NOT a duplicate question as I have tried ALL of other existing solutions on Stackoverflow and after spending 5 hours I am unable to get desired result.

I want to get two (or more) newly added files from a given directory in terms of date modified. The problem is that both the files are modified at the exact same time in terms of timestamp.

The file names look like:

data_london_20170515_0900.csv
data_toronto_20170515_0900.csv

EDIT after comments from expert developers:

Before trying to implement this solution, I was simply accessing files on a certain time, that is one minute AFTER when the files are copied. (So the script was running one minute after each newly copied file using Task Scheduler)

$date=date('Ymd');
$time = date('Hi', strtotime('-1 minutes')); // 1 minute less actual server time 
$date_time = $date."_".$time; // part of file name
$timeStamp_logFile = date("d/m/Y")." ".date("h:i:sa"); // DateTime used for file log entry

copy("//server/folder/data_london_".$date_time.".csv","c:/inetpub/wwwroot/folder/files/data_london_".$date_time.".csv");
copy("//server/folder/data_toronto_".$date_time.".csv","c:/inetpub/wwwroot/folder/files/data_toronto_".$date_time.".csv");

# Iterate through copied files and DB insertion

The above solution sometimes breaks because of minus one minute problem.

I have tried to implement this solution, but I was getting no output, just empty array. Also tried this solution, but same blank output.

The solution mentioned below is giving me no output:

$dir = "//server/folder/";

function scan_dir($dir) {
    $ignored = array('.', '..', '.svn', '.htaccess');

    $files = array();    
    foreach (scandir($dir) as $file) {
        if (in_array($file, $ignored)) continue;
        $files[$file] = filemtime($dir . '/' . $file);
    }

    arsort($files);
    $files = array_keys($files);

    return ($files) ? $files : false;
}

$f = scan_dir($dir);
print_r($f);

Any idea how to fix this problem?

Thanks.

18
  • 2
    "Could anyone please code the complete example" - No! SO is NOT a free coding service. We can help you with your existing code, but we're NOT here to code it all you! You need to show us what you've tried, example of the expected out put and what out put you actually got together with any potential error messages. Commented May 15, 2017 at 4:53
  • If you need something done but you can't do it yourself, is when you hire someone. I don't expect a mechanic to fix my car for free just because I don't know how to. Here are the guide lines when posting a question: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question? Commented May 15, 2017 at 4:57
  • Looking at your question history, posting an answer will just result in you not accepting it, but rather later write your own answer that you'll accept... Commented May 15, 2017 at 5:49
  • @MagnusEriksson I had read the exact same phrase on SO that I wrote here "Could anyone please code the complete example". I am failing to get the link as proof. About my history, that was a mistake and I rectified it. Commented May 15, 2017 at 6:17
  • 1
    The purpose of SO is to help each other with our existing code, if we get stuck. Not to give people a brief and expect them to build it all for you. Commented May 15, 2017 at 6:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.