note 95186 deleted from function.ftp-connect by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Dec 2009 15:46:25 +0000 |
| Subject: | note 95186 deleted from function.ftp-connect by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164091@lists.php.net to get a copy of this message | ||
Note Submitter: michael at no email dot org
----
There were some mistakes in sean at boyercentral dot net's ftp connection function. The regular
expression doesn't split the uri like shown in the example (the array element containing the
ftp protocol prefix is missing).
Plus, adding the ftp prefix oder directory adress to the host adress given to the ftp_connect
function may result in an error.
I adjusted his function to work properly:
<?php
function getFtpConnection($uri)
{
// Split FTP URI into:
// $match[0] = ftp://username:password@sld.domain.tld/path1/path2/
// $match[1] = username
// $match[2] = password
// $match[3] = sld.domain.tld
// $match[4] = /path1/path2/
preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $uri,
$match);
// Set up a connection
$conn = ftp_connect($match[3]);
// Login
if (ftp_login($conn, $match[1], $match[2]))
{
// Change the dir
ftp_chdir($conn, $match[4]);
// Return the resource
return $conn;
}
// Or retun null
return null;
}
?>