note 89811 added to function.ftp-connect
| From: | sean at boyercentral dot net | Date: | Tue, 24 Mar 2009 13:05:02 +0000 |
| Subject: | note 89811 added to function.ftp-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152378@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@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;
}
?>
----
Server IP: 216.235.13.26
Probable Submitter: 142.166.223.149
----
Manual Page -- http://www.php.net/manual/en/function.ftp-connect.php
Edit -- https://master.php.net/note/edit/89811
Del: integrated -- https://master.php.net/note/delete/89811/integrated
Del: useless -- https://master.php.net/note/delete/89811/useless
Del: bad code -- https://master.php.net/note/delete/89811/bad+code
Del: spam -- https://master.php.net/note/delete/89811/spam
Del: non-english -- https://master.php.net/note/delete/89811/non-english
Del: in docs -- https://master.php.net/note/delete/89811/in+docs
Del: other reasons-- https://master.php.net/note/delete/89811
Reject -- https://master.php.net/note/reject/89811
Search -- https://master.php.net/manage/user-notes.php