PHP 8.5.0 Alpha 1 available for testing

Voting

: min(eight, four)?
(Example: nine)

The Note You're Voting On

sean at boyercentral dot net
16 years ago
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:[email protected]/path1/path2/, and return an FTP connection resource.

<?php
function getFtpConnection($uri)
{
// Split FTP URI into:
// $match[0] = ftp://username:[email protected]/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;
}
?>

<< Back to user notes page

To Top