wiggst3r Posted June 20, 2008 Share Posted June 20, 2008 Hi I have an FTP server, that has files put onto it (supposedly daily, but not always). Each file has the name 'Transfer_20080620.txt' for example. The date changes depending on which day/month it is. As the files are put on the FTP server early in the morning, I'm looking to find the date of the last filename that is on the server (so I know the script is running properly when I get to work). What I'm looking to do, is find todays date (which I've done) and then loop through the file(names) and find the filename, which is closest to todays date. So, if there were files called 'Transfer_20080610.txt' , 'Transfer_20080612.txt', 'Transfer_20080613.txt', 'Transfer_20080614.txt', 'Transfer_20080617.txt', the loop would find the file named 'Transfer_20080617.txt' My code so far is: <?php function ftp_dwn($server,$user,$pass,$fromDir,$toDir) { $ftp_server = $server; $ftp_user_name = $user; $ftp_user_pass = $pass; $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if((!$conn_id) || (!$login_result)) { echo "Could not Connect to the FTP Server"; exit(); } if(!file_exists($toDir)) // If our local directory doesn't exists let's create it mkdir($toDir); $contents = ftp_nlist($conn_id, $fromDir); // Compiles an array of all given folders in remote structure } print $contents; // Initialize FTP Server $ftp_server = "www.example.com"; $ftp_user_name = "xxxxxxx"; $ftp_user_pass = "xxxxxx"; $fromDir = "folder"; $toDir = "/Users/mac/workspace/test_ftp"; ftp_dwn($ftp_server,$ftp_user_name,$ftp_user_pass,$fromDir,$toDir); $today = date("Ymd"); print $today; ?> Thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 20, 2008 Share Posted June 20, 2008 You know that there will never be a file on the FTP server that is there after today's date, so all you really have to do is find the latest file (by date) on the server. Also, since the mechanism to get the list of files from the FTP server is only done once, you really don't need the overhead of a function. <?php $ftp_server = "www.example.com"; $ftp_user_name = "xxxxxxx"; $ftp_user_pass = "xxxxxx"; $fromDir = "folder"; $toDir = "/Users/mac/workspace/test_ftp"; $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if((!$conn_id) || (!$login_result)) { echo "Could not Connect to the FTP Server"; exit(); } if(!file_exists($toDir)) // If our local directory doesn't exists let's create it mkdir($toDir); $contents = ftp_nlist($conn_id, $fromDir); // Compiles an array of all given folders in remote structure sort($contents); $last_file = $contents[count($contents) - 1]; echo 'The last file created was ' . $last_file . "<br>\n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
wiggst3r Posted June 20, 2008 Author Share Posted June 20, 2008 Hi Thanks for that There's a problem though. I have a folder named 'import', which was created on 17/12/2007 and the code you gave me, outputs The last file created was import It should say The last file created was Transfer_20080619.txt Any ideas why? Quote Link to comment Share on other sites More sharing options...
wiggst3r Posted June 20, 2008 Author Share Posted June 20, 2008 It only works when I use: $last_file = $contents[count($contents) - 23 ]; Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 20, 2008 Share Posted June 20, 2008 It's because the array also contains folder names. Is "import" the only folder placed along with the txt files? If so, you could simply remove it from the array before sorting: <?php $ftp_server = "www.example.com"; $ftp_user_name = "xxxxxxx"; $ftp_user_pass = "xxxxxx"; $fromDir = "folder"; $toDir = "/Users/mac/workspace/test_ftp"; $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if((!$conn_id) || (!$login_result)) { echo "Could not Connect to the FTP Server"; exit(); } if(!file_exists($toDir)) // If our local directory doesn't exists let's create it mkdir($toDir); $contents = ftp_nlist($conn_id, $fromDir); // Compiles an array of all given folders in remote structure unset($contents[array_search('import', $contents)]); sort($contents); $last_file = end($contents); echo 'The last file created was ' . $last_file . "<br>\n"; ?> Quote Link to comment Share on other sites More sharing options...
wiggst3r Posted June 20, 2008 Author Share Posted June 20, 2008 Theres 2 folders One called import and one called archive. How would I remove this folder too? Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 20, 2008 Share Posted June 20, 2008 Just add unset($contents[array_search('archive', $contents)]); after the first unset(). Quote Link to comment Share on other sites More sharing options...
wiggst3r Posted June 20, 2008 Author Share Posted June 20, 2008 The code you gave me works fine, as it finds the file at the bottom of the folder. But it returns a file, that was created on 17/05/2008 I'd like it to loop through the files on the FTP and find the file that matches the string Transfer_20080617.txt, for example. Where Transfer_20080617.txt, was the last file that was uploaded before todays date. There are other files and folders on the FTP, but I'm only concerned with finding the files with the Transfer_2008xxxx.txt. Any ideas? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 20, 2008 Share Posted June 20, 2008 Ah, I see. It would be easier if the folder just had the "Transfer_*.txt" files, but it's really easy to fix. Instead of removing elements from the array, we can sort it (from highest to lowest - newest "Transfer_*.txt file occurring first), loop through it, and exit the loop if a file beginning with "Transfer_" is found (the single file you're looking for): <?php $ftp_server = "www.example.com"; $ftp_user_name = "xxxxxxx"; $ftp_user_pass = "xxxxxx"; $fromDir = "folder"; $toDir = "/Users/mac/workspace/test_ftp"; $conn_id = ftp_connect($ftp_server); $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); if((!$conn_id) || (!$login_result)) { echo "Could not Connect to the FTP Server"; exit(); } if(!file_exists($toDir)) // If our local directory doesn't exists let's create it mkdir($toDir); $contents = ftp_nlist($conn_id, $fromDir); // Compiles an array of all given folders in remote structure //sort file array sort($contents); //reverse array to highest to lowest $sorted = array_reverse($contents); //loop through array, and break (exit loop) when a file beginning with 'Transfer_' is found, while the filename is saved in $last_file foreach ($sorted as $file) { if (substr($file, 0, 9) == 'Transfer_') { $last_file = $file; break; } } echo 'The last file created was ' . $last_file . "<br />\n"; ?> Quote Link to comment 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.