How to repeat PHP script with updated variables

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Slimat
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Jul 07, 2017 8:37 am

Tue Nov 06, 2018 1:01 pm

I am using a PHP script to download multiple files from various FTP servers.

This is the working script;
// Download Data File

// FTP server details
$ftp_host = 'ftp.address.com'; // Address of FTP server
$ftp_username = 'username'; // Username
$ftp_password = 'password'; // Password

// open an FTP connection
$conn_id = ftp_connect($ftp_host) or die("<b><font color='red' face='Arial'>Couldn't connect to $ftp_host</face></font color></b></br>");

// login to FTP server
$ftp_login = ftp_login($conn_id, $ftp_username, $ftp_password);

// local & server file path
$localFilePath = '/var/www/html/datafiles/ftp.csv';
$remoteFilePath = 'ftp.csv';

// try to download a file from server
if(ftp_get($conn_id, $localFilePath, $remoteFilePath, FTP_BINARY)){
echo "<font color='green' face='Arial'>File transfer successful - $localFilePath</face></font color></br>";
}else{
echo "<b><font color='red' face='Arial'>There was an error while downloading $localFilePath</face></font color></b></br>";
}
// close the connection
ftp_close($conn_id);
However... now I have to download at least 30 files every hour and you can imagine my PHP script is ridiculously long as I have to repeat the above script with different variables for each datafile.

So, to tidy it up I wanted to store the following variables in a SQL database;

$ftp_host
$ftp_username
$ftp_password
$localFilePath
$remoteFilePath

Then I want to load one set of variables, run the script, load the next set of variables and repeat the script until all the datafile have been downloaded.

I have created a variables table in MySQL and stored the details in fields called;

ftp_host
ftp_username
ftp_password
localFilePath
remoteFilePath

I was wondering if anyone can point me someway to how I create this loop and get the script to repeat for each FTP server... I am not sure what terminology I should be searching for but would appreciate being pointed to where to start with this.

I did wonder if it would be simpler to do this in an array... but again I know nothing about arrays or where to start...

Many thanks in advance :)

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 747
Joined: Mon Feb 22, 2016 5:52 pm

Tue Nov 06, 2018 1:17 pm

I think that your biggest problem could be with the script timing out, not sure as it depends where you are getting the files from?

It might be a solution to set up a cron job rather than a loop to execute the script, to do the transfers and mark the database when the file has been successfully downloaded?

Slimat
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Jul 07, 2017 8:37 am

Tue Nov 06, 2018 1:54 pm

Hi hyper

Thanks for replying... the script works fine - there are no problems with it at all - I am just trying to shorten the length of the code as my PHP script is now about 800 lines long with all the other parts... so I am just looking for a way to shorten it.

I thought I had found a way just now using foreach - when I read up on it just now I created this code, but it didn't work... but I am not sure why and am playing with it now to try and get it working - any ideas gratefully received;
// Download data files from suppliers FTP servers

// Create array with FTP server credentials
// Order - $ftp_host, $ftp_username, $ftp_password, $localFilePath, $remoteFilePath
$array = [
['ftp.server1.com', 'username1', 'password1', '/var/www/html/datafiles/datafile1.csv', 'datafile1.csv'], // Supplier1
['ftp.server2.com', 'username2', 'password2', '/var/www/html/datafiles/datafile2.csv', 'datafile2.csv'], // Supplier1

];

foreach ($array as list($ftp_host, $ftp_username, ftp_password, $localFilePath, $remoteFilePath)) {

// open an FTP connection
$conn_id = ftp_connect($ftp_host) or die("<b><font color='red' face='Arial'>Couldn't connect to $ftp_host</face></font color></b></br>");

// login to FTP server
$ftp_login = ftp_login($conn_id, $ftp_username, $ftp_password);

// try to download a file from server
if(ftp_get($conn_id, $localFilePath, $remoteFilePath, FTP_BINARY)){
echo "<font color='green' face='Arial'>File transfer successful - $localFilePath</face></font color></br>";
}else{
echo "<b><font color='red' face='Arial'>There was an error while downloading $localFilePath</face></font color></b></br>";
}

// close the connection
ftp_close($conn_id);

}

Slimat
New php-forum User
New php-forum User
Posts: 4
Joined: Fri Jul 07, 2017 8:37 am

Tue Nov 06, 2018 3:08 pm

OK, I managed to shrink the length of my PHP script by about 500 lines by using the following script to download from multiple FTP servers - I have entered the multiple server credentials in $array and then looped these details through the download script using a foreach loop.

Here is the working (much easier to administer) script - just in case anyone else is looking for something similar;

Code: Select all

<?PHP
// Download data files from suppliers FTP servers

// Create array with FTP server credentials
$array = [

   ["ftp.server-address1.com", "ftp1-username", "ftp1-password", "/var/www/html/datafiles/ftp1-datafile.csv", "ftp1-datafile.csv"], // FTP1
   ["ftp.server-address2.com", "ftp2-username", "ftp2-password", "/var/www/html/datafiles/ftp2-datafile.csv", "ftp2-datafile.csv"], // FTP2
   ["ftp.server-address3.com", "ftp3-username", "ftp3-password", "/var/www/html/datafiles/ftp3-datafile.csv", "ftp3-datafile.csv"], // FTP3

];

foreach ($array as list($ftp_host, $ftp_username, $ftp_password, $localFilePath, $remoteFilePath)) {
	echo "$ftp_host, $ftp_username, $ftp_password, $localFilePath, $remoteFilePath</br>";

// open an FTP connection
	$conn_id = ftp_connect($ftp_host) or die("<b><font color='red' face='Arial'>Couldn't connect to $ftp_host</face></font color></b></br>");

// login to FTP server
	$ftp_login = ftp_login($conn_id, $ftp_username, $ftp_password);
	
// try to download a file from server
	if(ftp_get($conn_id, $localFilePath, $remoteFilePath, FTP_BINARY)){
		echo "<font color='green' face='Arial'>File transfer successful - $localFilePath</face></font color></br>";
	}else{
		echo "<b><font color='red' face='Arial'>There was an error while downloading $localFilePath</face></font color></b></br>";
	}

// close the connection
	ftp_close($conn_id);
    
}
?>
Hope this helps someone ;)

Post Reply