note 103431 deleted from function.ftp-connect by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Apr 2011 04:13:42 +0000 |
| Subject: | note 103431 deleted from function.ftp-connect by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-178786@lists.php.net to get a copy of this message | ||
Note Submitter: larry at bloo dot com dot au
----
I really liked "sean at boyercentral dot net"'s reg_ex for splitting out the complete
URI (with dir), but it had a few problems with the array positions...
here is a revised edition:
<?php
function getFtpConnection($uri)
{
/*
Split FTP URI into:
array(5) {
[0]=>
string(48) "ftp://user:password@ftp.myftptest.com/remotedir/"
[1]=>
string(4) "user"
[2]=>
string(8) "password"
[3]=>
string(17) "ftp.myftptest.com"
[4]=>
string(11) "/remotedir/"
}
*/
preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $uri,
$uriArr);
list($src,$usr,$pwd,$url,$dir) = $uriArr;
// Set up a connection
$conn = ftp_connect($url);
// Login
if (ftp_login($conn, $usr, $pwd))
{
// Change the dir
ftp_chdir($conn, $dir);
// Return the resource
return $conn;
}
// Or retun null
return null;
}
?>
example usage:
<?php
$conn_rsrc = getFtpConnection('ftp://user:password@ftp.myftptest.com/remotedir/');
?>