How to Check FTP Connection with PHP for a List Of Sites

By | October 17, 2008

As you might know, PHP has some built-in FTP functions. If you need to check a batch of sites for FTP connection, you can do it with a PHP script. This should be necessary if you have some sites on heavy servers to check them for response, or for anything else you might need. Let me show you the script itself.

// Checking whether the list of sites was entered
if (isset($sitest))
echo “Starting…rn”;
// Creating an array with our FTP data
$fetepe=explode(“rn”, $sitest);
// Starting a loop that checks all the FTP accounts we’ve given to the script
foreach ($fetepe as $oprea)
// Creating a variable that will contain successful entries
// Creating an array from the string
$noua=explode(“:”, $oprea);
// Getting the name of the site (May be a domain or subdomain)
// Getting host, login and password: I think everything is clear
// Trying to connect to an FTP server suppressing errors
$conn_id = @ftp_connect($host, 21, 10);

// Logging in with username and password
$login_result = @ftp_login($conn_id, $login, $password);

// Checking the connection
if ((!$conn_id) || (!$login_result)) {
} else {
// If everything is OK, we’re creating a record that is easy for import in CuteFTP Pro
$tow=”Site Label: “.$dom.”rn”;
$tow.=”HostName: “.$host.”rn”;
$tow.=”Username: “.$login.”rn”;
$tow.=”Password: “.$password.”rn”;
$tow.=”Port: 21nUses passive modernrn”;
// Eching the string we’ve written to the file
echo $tow;
// Writing the valid pair to a file
$as=fopen (“ftp.txt”, “a”);
fwrite($as, $tow);

// You can find the form below.
<form action=”” method=”post”>
<textarea name=”sitest” cols=”70″ rows=”5″></textarea>
<br /><input type=”submit” name=”Submit” value=”Check” />

Let’s explain what we’re doing in this script. First of all we need to have a list of FTP access information in the following format: hostname:sitename:login:password. Then we’re checking this list: for each entry we’re getting the connection and login information and are trying to connect. If the connection seems to be OK, we’re generating a valid FTP account data in the format of CuteFTP Professional – a good software (It’s a pity it’s not free, but it’s worth the money). Then this information is written to the file and then all we have is to run import within the software, and we’re having a list of sites in CuteFTP.

You’re welcome to create your own format for your needs. I think that it’s better when the list is easy to import. If you’re using any other software, you’re welcome to store valid accounts in any other format you like.