note 89811 modified in function.ftp-connect by danbrown
| From: | danbrown@php.net | Date: | Tue, 24 Mar 2009 13:15:22 +0000 |
| Subject: | note 89811 modified in function.ftp-connect by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152382@lists.php.net to get a copy of this message | ||
Ever needed to create an FTP connection resource defaulted to a particular dir from a URI?
Here's a simple function that will take a URI like ftp://username:password@subdomain.example.com/path1/path2/,
and return an FTP connection resource.
<?php
function getFtpConnection($uri)
{
// Split FTP URI into:
// $match[0] = ftp://username:password@sld.domain.tld/path1/path2/
// $match[1] = ftp://
// $match[2] = username
// $match[3] = password
// $match[4] = sld.domain.tld
// $match[5] = /path1/path2/
preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $uri,
$match);
// Set up a connection
$conn = ftp_connect($match[1] . $match[4] . $match[5]);
// Login
if (ftp_login($conn, $match[2], $match[3]))
{
// Change the dir
ftp_chdir($conn, $match[5]);
// Return the resource
return $conn;
}
// Or retun null
return null;
}
?>
--was--
Ever needed to create an FTP connection resource defaulted to a particular dir from a URI?
Here's a simple function that will take a URI like ftp://username:password@sld.domain.tld/path1/path2/,
and return an FTP connection resource.
<?php
function getFtpConnection($uri)
{
// Split FTP URI into:
// $match[0] = ftp://username:password@sld.domain.tld/path1/path2/
// $match[1] = ftp://
// $match[2] = username
// $match[3] = password
// $match[4] = sld.domain.tld
// $match[5] = /path1/path2/
preg_match("/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i", $uri,
$match);
// Set up a connection
$conn = ftp_connect($match[1] . $match[4] . $match[5]);
// Login
if (ftp_login($conn, $match[2], $match[3]))
{
// Change the dir
ftp_chdir($conn, $match[5]);
// Return the resource
return $conn;
}
// Or retun null
return null;
}
?>
http://php.net/manual/en/function.ftp-connect.php