Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I use FTP from PHP?
#1
Bug 
One of the great things about PHP is the sheer amount of functionality that’s either built into it, or is only an extension away. File Transfer Protocol (FTP) is a great example of such functionality.

Solutions

Here are two popular approaches that you can take to using FTP from PHP.
Using PHP’s Built-in FTP Functions

You can use PHP’s FTP functionality to have PHP scripts act as clients to an FTP server. This can be useful for countless tasks, whether you’re building a web interface for an FTP file repository, or developing a tool to update your site from your PHP development environment. In order to use the FTP functions, you’ll need to make sure your host has enabled PHP’s FTP functionality.

In this example, we use PHP’s FTP functionality to connect to an FTP server and list the files in a directory:


Quote:<?php
set_time_limit(0);
$ftpServer = 'localhost';
$targetDir = '/';
if (!$fp = ftp_connect($ftpServer, 21, 30))
{
die('Connection failed');
}
if (!ftp_login($fp, 'anonymous', '[email protected]'))
{
die('Login failed');
}
if (!ftp_chdir($fp, $targetDir))
{
die ('Unable to change directory to: ' . $targetDir);
}
echo "<pre>Current Directory:" . ftp_pwd($fp) .
"\n\n";
echo "Files Available:\n";
$files = ftp_nlist($fp, '/');
foreach ($files as $file)
{
echo $file . "\n";
}
echo '</pre>';
?>
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)