|
|
XXXVII. FTP 関数
この拡張モジュールの関数は、http://www.faqs.org/rfcs/rfc959.htmlで定義された File Transfer
Protocol (FTP)を使用してファイルサーバにアクセスするクライアントの
実装です。
これらの関数は、標準モジュールの一部として利用可能であり、常に使用できます。
PHPでFTP関数を使用するには、PHP 4をインストール際には
--enable-ftpオプション、PHP 3を使
用する場合には--with-ftpを追加する
必要があります。
Windows版のPHPには
この拡張モジュールのサポートが組み込まれています。これらの関数を使用
するために拡張モジュールを追加でロードする必要はありません。 この拡張モジュールは設定ディレクティブを全く定義しません。
この拡張モジュールは、1種類のリソース型を使用します。このリソース
型は、FTP接続のリソースIDで、ftp_connect()によ
り返されたものです。
これらの定数は、この拡張モジュールで定義されており、
この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー
ドされるかのどちらかの場合のみ使用可能です。
以下の定数は、PHP 4.3.0で追加されました。
- FTP_AUTOSEEK
(integer)
詳細は、ftp_set_option() を参照して下さい。
- FTP_AUTORESUME
(integer)
GETおよびPUTリクエスト用のレジューム位置と開始位置を自動的に定義
します。(FTP_AUTOSEEKが有効な場合のみ動作します。)
- FTP_FAILED
(integer)
非同期伝送が失敗しました。
- FTP_FINISHED
(integer)
非同期伝送が終了しました。
- FTP_MOREDATA
(integer)
非同期伝送がまだアクティブです。
例 1. FTPの例
<?php
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) {
echo "Ftp connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
die;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name";
}
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);
if (!$upload) {
echo "Ftp upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}
ftp_close($conn_id);
?>
|
|
arjen at queek dot nl
15-Jul-2004 02:38
If you prefer a OO-approach to the FTP-functions, you can use this snippet of code (PHP5 only! and does add some overhead). It's just a "start-up", extend/improve as you wish...
You can pass all ftp_* functions to your object and stripping ftp_ of the function name. Plus, you don't have to pass the ftp-resource as the first argument.
For example:
<?php
ftp_delete($ftp, $file); ?>
Can become:
<?php
$ftp->delete($file); ?>
Code:
<?php
class FTP {
private $ftp;
public function __construct($host, $port = 21, $timeout = 90) {
$this->ftp = ftp_connect($host, $port, $timeout);
}
public function __destruct() {
@ftp_close($this->ftp);
}
public function __call($function, $arguments) {
array_unshift($arguments, $this->ftp);
return call_user_func_array('ftp_' . $function, $arguments);
}
}
?>
Kristy Christie (kristy at isp7 dot net)
15-Jun-2004 06:50
Here's a little function that I created to recurse through a local directory and upload the entire contents to a remote FTP server.
In the example, I'm trying to copy the entire "iwm" directory located at /home/kristy/scripts/iwm to a remote server's /public_html/test/ via FTP.
The only trouble is that for the line "if (!ftp_chdir($ftpc,$ftproot.$srcrela))", which I use to check if the directory already exists on the remote server, spits out a warning about being unable to change to that directory if it doesn't exist.
But an error handler should take care of it.
My thanks to the person who posted the snippet on retrieving the list of files in a directory.
For the version of the script that echo's it's progress as it recurses & uploads, go to:
<?php
$ftproot = "/public_html/test/";
$srcroot = "/home/kristy/scripts/";
$srcrela = "iwm/";
$ftpc = ftp_connect("ftp.mydomain.com");
$ftpr = ftp_login($ftpc,"username","password");
if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }
ftpRec ($srcrela);
ftp_close($ftpc);
function ftpRec ($srcrela)
{
global $srcroot;
global $ftproot;
global $ftpc;
global $ftpr;
chdir($srcroot.$srcrela);
if (!ftp_chdir($ftpc,$ftproot.$srcrela))
{
ftp_mkdir ($ftpc,$ftproot.$srcrela);
ftp_chdir ($ftpc,$ftproot.$srcrela);
}
if ($handle = opendir("."))
{
while (false !== ($fil = readdir($handle)))
{
if ($fil != "." && $fil != "..")
{
if (!is_dir($fil))
{
ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
}
else
{
if ($fil == "templates")
{
}
else
{
ftpRec ($srcrela.$fil."/");
chdir ("../");
}
}
}
}
closedir($handle);
}
}
?>
postmaster at alishomepage dot com
24-Jan-2004 11:29
I have written an OpenSource ZIP2FTP interface, which actually takes a given ZIP file and decompresses it in the folder on an FTP server you specify...
Therefore it may be quite interesting for you people interested in FTP, its adress is ; those who directly want the source may visit
Vikrant Korde <vakorde at hotmail dot com>
14-Nov-2003 01:35
<?
function rec_copy ($source_path, $destination_path, $con)
{
ftp_mkdir($con, $destination_path);
ftp_site($con, 'CHMOD 0777 '.$destination_path);
ftp_chdir($con,$destination_path);
if (is_dir($source_path))
{
chdir($source_path);
$handle=opendir('.');
while (($file = readdir($handle))!==false)
{
if (($file != ".") && ($file != ".."))
{
if (is_dir($file))
{
if($file != "propertyimages")
{
rec_copy ($source_path."/".$file, $file, $con);
chdir($source_path);
ftp_cdup($con);
}
}
if (is_file($file))
{
$fp = fopen($file,"r");
ftp_fput ($con, str_replace(" ", "_", $file), $fp,FTP_BINARY);
ftp_site($con, 'CHMOD 0755 '.str_replace(" ", "_", $file));
}
}
}
closedir($handle);
}
}
$con = ftp_connect("69.18.213.131",21);
$login_result = ftp_login($con,"username","password");
$rootpath = "mainwebsite_html";
$sourcepath = realpath("../")."/resdesk";
$destination_dir_name = "resdesk_".$account_id."/";
rec_copy ($sourcepath, $destination_dir_name, $con);
if (function_exists("ftp_close"))
{
ftp_close($con);
}
?>
postmaster at alishomepage dot com
24-Oct-2003 11:06
Here's another FTP interface over PHP (also uses MySQL)
PS: this script will ALSO allow you to download its source... So it becomes interesting for YOU PROGRAMMERS as well :D
arjenjb dot wanadoo dot nl
09-Mar-2003 06:29
Check for a FTP server written in PHP.
Supports Passive and Active FTP, and all other standard FTP commands as decribed in RFC959.
NOSPAMkent at ioflux dot NOSPAM dot com
20-Sep-2002 12:05
I think what some other posts were trying to say which may need clarification is that in PHP 4.2.3, ftp_connect("myhost.com") was failing most of the time, except it would work like every few minutes.
The fix is that ftp_connect seems to have a bug resolving addresses. If you do:
$hostip = gethostbyname($host);
$conn_id = ftp_connect($hostip);
It seems to solve the problem.
(Other users referred to an ftpbuf() error... not sure what that is, but this should fix it.)
sven at cartell-network dot de
13-Feb-2002 07:27
connection to a ftp server across proxy
$ftp_server = "proxy"; f.e. 123.456.789.10
$ftp_user_name = "username@ftpserver"; f.e. [email protected]
$ftp_user_pass = "password";
$conn_id = ftp_connect($ftp_server, 2121);
$login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
| |