|
użytkowników online: 28
|
OPINIE UŻYTKOWNIKÓW
|
Z mojej strony serwisowi należy się bardzo mocna pochwała. Nawet późna pora zgłoszenia problemu (23.00) nie przeszkodziła Darkowi w jego rozwiązaniu. Do tego poziom odpisywania na maile jest bardzo wysoki... wszystko wykłada jak cierpliwy nauczyciel. Śmiało mogę przyznać, że zamieszczone na stronach porady są rzeczowo opisane - a nie jak to bywa w innych serwisach mamy sam kod i nic poza tym! Jeszcze raz wielkie dzięki!
Damian Jarosz
Adminer.pl
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
XXXIX. FTP
Funkcje z tego rozszerzenia implementują kliencki dostęp do plików serwera
rozpoznającego File Transfer Protocol (FTP) opisanego w
http://www.faqs.org/rfcs/rfc959. To rozszerzenie
umożliwia dostęp do serwera FTP udostępniając szeroki
zakres możliwości kontoli dla wykonującego skryptu. Jeśli chcesz
tylko czytać z pliku na serwerze FTP lub do niego zapisywać, zastanów
się nad użyciem wrappera ftp://
z funkcjami systemu plików
które udostępniaja prostszy i bardziej intuicyjny interfejs.
Do zbudowania tego rozszerzenia nie są wymagane
żadne zewnętrzne biblioteki. To rozszerzenie nie definiuje posiada żadnych
dyrektyw konfiguracyjnych w pliku php.ini.
Poniższe stałe są zdefiniowane w tym rozszerzeniu i stają się dostępne, gdy
rozszerzenie jest dokompilowane do PHP, lub załadowane dynamicznie przy starcie.
Przykład 1. Przykład użycia 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 "Połączenie FTP się nie powiodło!";
echo "Próbowano połączyć się do $ftp_server jako użytkownik"
. $ftp_user_name;
exit;
} else {
echo "Połączony z $ftp_server jako użytkownik $ftp_user_name";
}
$upload = ftp_put($conn_id, $plik_docelowy, $plik_zrodlowy, FTP_BINARY);
if (!$upload) {
echo "Wstawienie pliku przez FTP się nie powiodło!";
} else {
echo "Przesłano $plik_zrodlowy na $ftp_server jako $plik_docelowy";
}
ftp_close($conn_id);
?>
|
|
- Spis treści
- ftp_alloc -- Allocates space for a file to be uploaded
- ftp_cdup -- Zmienia bieżący katalog na nadrzędny
- ftp_chdir -- Zmienia katalog na serwerze FTP
- ftp_chmod -- Set permissions on a file via FTP
- ftp_close -- Zamyka połączenie FTP
- ftp_connect -- Otwiera połączenie FTP
- ftp_delete -- Usuwa plik z serwera FTP
- ftp_exec -- Żądaj wywołania programu na serwerze FTP
- ftp_fget --
Pobiera plik z serwera FTP i zapisuje go do otwartego pliku.
- ftp_fput -- Umieszcza dane z otwartego pliku na serwerze FTP
- ftp_get_option -- Pobiera różne opcje startowe strumienia FTP
- ftp_get -- Pobiera plik z serwera FTP
- ftp_login -- Loguje się w połączeniu FTP
- ftp_mdtm -- Zwraca czas ostaniej modyfikacji podanego pliku
- ftp_mkdir -- Tworzy katalog
- ftp_nb_continue -- Continues retrieving/sending a file (non-blocking)
- ftp_nb_fget -- Retrieves a file from the FTP server and writes it to an open file (non-blocking)
- ftp_nb_fput -- Stores a file from an open file to the FTP server (non-blocking)
- ftp_nb_get -- Retrieves a file from the FTP server and writes it to a local file (non-blocking)
- ftp_nb_put -- Stores a file on the FTP server (non-blocking)
- ftp_nlist -- Zwraca listę plików w podanym katalogu
- ftp_pasv -- Włącza/wyłącza tryb pasywny
- ftp_put -- Umieszcza plik na serwerze FTP
- ftp_pwd -- Zwraca nazwę bieżącego katalogu
- ftp_quit -- Zamyka połączenie FTP
- ftp_raw -- Sends an arbitrary command to an FTP server
- ftp_rawlist --
Zwraca szczegółową listę plików w podanym katalogu.
- ftp_rename -- Zmienia nazwę pliku na serwerze FTP.
- ftp_rmdir -- Usuwa katalog
- ftp_set_option -- Ustaw różne opcje startowe FTP
- ftp_site -- Wysyła serwerowi polecenie SITE.
- ftp_size -- Zwraca rozmiar podanego pliku.
- ftp_ssl_connect -- Opens an Secure SSL-FTP connection
- ftp_systype --
Zwraca identyfikator systemu dla zdalnego serwera FTP.
User Contributed Notesmagic2lantern at mail dot ru
23-Dec-2005 05:19
This is a simple script writen by me which allows the user
run it in console and get tree from FTP Server.
<?php
$argv = $_SERVER["argv"];
$argv = array_slice($argv,1);
if (getenv("HTTP_HOST")) {exit("For bash execution only!");}
if ((empty($argv[0])) || (empty($argv[1]))) {exit("Usage: thisscript.php 10.0.0.10 /games/Arcade/
where 10.0.0.10 is the FTP Server and /games/Arcade is the start path on FTP\n");}
$connect_id = ftp_connect($argv[0]);
if ($connect_id) { $login_result = ftp_login($connect_id, 'anonymous', 'JustForFun'); }
if ((!$connect_id) || (!$login_result)) {
echo "Unable to connect the remote FTP Server!\n";
exit;
}
function ftp_get_dir ($ftp_dir) {
global $connect_id;
$ftp_contents = ftp_nlist ($connect_id, $ftp_dir);
for ($myCounter_for_server_elements = 0; !empty ($ftp_contents[$myCounter_for_server_elements]);
$myCounter_for_server_elements++)
{
echo $argv[0] . $ftp_dir . $ftp_contents[$myCounter_for_server_elements] . "\n";
ftp_get_dir ($ftp_dir . $ftp_contents[$myCounter_for_server_elements] . '/');
}
}
ftp_get_dir ($argv[1]);
ftp_close($connect_id);
?>
leonardo at rol dot com dot ro
09-Aug-2005 03:51
Here is an example for downloading a remote ftp structure to local server, useful for migrating sites from one server to another, especially when you are upgrading ensim ;)
http://leonardo.rol.ro/ftpleech.txt
dickiedyce at uk dot com
07-May-2005 05:20
It may seem obvious to others, but it had me stumped for nearly an hour! If you can connect to an ftp site but some functions (list, put, get etc) don't work, then try using ftp_pasv and set passive mode on.
<?php
$hostip = gethostbyname($host);
$conn_id = ftp_connect($hostip);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
ftp_pasv ( $conn_id, true );
if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $host for user $ftp_user_name";
die;
} else {
echo "Connected to $host, for user $ftp_user_name<br>";
echo "Host IP is $hostip<br>";
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file<br>";
} else {
echo "There was a problem while uploading $file<br>";
}
ftp_close($conn_id);
}
?>
arjen at queek dot nl
15-Jul-2004 04: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 08: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: http://pastebin.com/73784
<?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 12:29
Vikrant Korde <vakorde at hotmail dot com>
14-Nov-2003 02: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 01:06
Here's another FTP interface over PHP (also uses MySQL)
http://myftp.alishomepage.com
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 07:29
Check http://nanoftpd.sourceforge.net/ 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 02: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 08:27
connection to a ftp server across proxy
$ftp_server = "proxy"; f.e. 123.456.789.10
$ftp_user_name = "username@ftpserver"; f.e. exampleuk@www.example.uk
$ftp_user_pass = "password";
$conn_id = ftp_connect($ftp_server, 2121);
$login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
|