Hi
I'm trying to download some files using the FTP functions and a while loop.
The files are in the format of File_name_date.txt, where the date is in the format 20080612, for example.
I could probably download all the files, but what I'm looking to do, is after each file is downloaded, to run a function, that processes the files contents.
The script I'm doing, is that it searches a mysql database, for the last time a row was inserted. So if the date was 11th june, the date in the database will be 20080611.
If the date is in the database, it means the file named File_name_20080611.txt has been downloaded
The script must then connect to the FTP and search for the file one day after, so it then looks for File_name_20080612.txt, and downloads that.
As the files are not put on the FTP daily, it misses off days, so the script must, loop round, adding one day to the previous day and must find the next date, in which to download a file.
What my code does so far, is that it searches the mysql database, finds the last inserted date, sets the file to search for to be one day after. That's it. As the file File_name_20080612.txt, is not in the database, it throws the error
File_name_20080612.txt: The system cannot find the file specified
What it needs to do, is if it can't find this file, it must then loop round until it does find a file on the FTP.
My code so far is:
<?php
require_once('config.php');
require_once('dbconn.php');
start_trace('run.php');
function run()
{
$fct_name = 'run';
function_start($fct_name);
$up_to_date = false;
// db select
$result = mysql_query("SELECT date FROM csv_files ORDER BY date DESC LIMIT 0, 1")
or die(mysql_error());
$row = mysql_fetch_array( $result );
$last_inserterd_date = $row['date'];
debug("LAST INSERTED [$last_inserterd_date]");
$next_date = date('Y-m-d', (strtotime($last_inserterd_date)+ 86400));
debug(" NEXT DATE IS [$next_date]");
if ($next_date == date('Y-m-d'))
{
$up_to_date = true;
}
else
{
// connect to FTP
$ftp_server = "www.example.com";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$fromDir = "folder/";
$toDir = "/Users/mac/workspace/download_csv/download/";
// Connected, else throw an exception
$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 (ftp_chdir($conn_id, "folder/"))
{
//changed to the directory - debug stuff
}
else
{
// couldn't change directory - debug stuff
}
while(!$up_to_date)
{
$file_to_search_for = "File_name_" . date('Ymd', strtotime($next_date)) . ".txt";
debug("The file to search for is [$file_to_search_for]");
if (ftp_get($conn_id, $file_to_search_for, $file_to_search_for, FTP_ASCII, 0))
{
echo "successfully written to $file_to_search_for\n";
}
else
{
echo "Cannot find $file_to_search_for to $file_to_search_for\n";
}
// set $next_date to be $next_date + 1 day
$next_date = date('Ymd', strtotime($next_date) + (24*60*60));
$file_to_search_for = "File_name_" . date('Ymd', strtotime($next_date)) . ".txt";
// if $next_date > today
$today = date('Ymd');
if ($next_date > $today)
{
$up_to_date = true;
}
else
{
$up_to_date = false;
}
//check FTP for date $file_to_search_for
//if file exists
//download file and run
//
//set $next_date to be $next_date + 1 day
// if $next_date > today
$up_to_date = true;
//end
}
function_end($fct_name);
}
run();
end_trace('run.php');
?>
If anyone has any ideas, that would be great.
Thanks