Upload file to server via ftp.
<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
exit;
} else {
echo "There was a problem while uploading $file\n";
exit;
}
// close the connection
ftp_close($conn_id);
?>
FTP
- Wstęp
- Instalacja/Konfiguracja
- Stałe predefiniowane
- Przykłady
- FTP Funkcje
- ftp_alloc — Allocates space for a file to be uploaded
- ftp_cdup — Zmienia bieżący katalog na nadrzędny
- ftp_chdir — Zmienia bieżący katalog na serwerze FTP
- ftp_chmod — Set permissions on a file via FTP
- ftp_close — Zamyka połączenie FTP
- ftp_connect — Opens an FTP connection
- ftp_delete — Usuwa plik na serwerze FTP
- ftp_exec — Żąda wykonanie polecenia przez serwer FTP
- ftp_fget — Pobiera plik z serwera FTP i zapisuje do otwartego pliku
- ftp_fput — Wysyła dane z otwartego pliku na serwer FTP
- ftp_get_option — Pobiera ustawienia dotyczące strumienia FTP
- ftp_get — Pobiera plik z serwera FTP
- ftp_login — Loguje się w ramach połączenia FTP
- ftp_mdtm — Zwraca czas ostatniej 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 z podanego katalogu
- ftp_pasv — Włącza lub wyłącza tryb pasywny
- ftp_put — Przesyła plik na serwer FTP
- ftp_pwd — Zwraca nazwę katalogu bieżącego
- ftp_quit — Alias dla ftp_close
- ftp_raw — Sends an arbitrary command to an FTP server
- ftp_rawlist — Zwraca szczegółową listę plików z podanego katalogu
- ftp_rename — Zmienia nazwę pliku lub katalogu na serwerze FTP
- ftp_rmdir — Usuwa katalog
- ftp_set_option — Set miscellaneous runtime FTP options
- ftp_site — Wysyła polecenie SITE do serwera FTP
- ftp_size — Zwraca rozmiar podanego pliku
- ftp_ssl_connect — Opens an Secure SSL-FTP connection
- ftp_systype — Zwraca identyfikator typu systemu dla zdalnego serwera FTP
asifkhandk at gmail dot com ¶
2 months ago
tendrid at gmail dot com ¶
1 year ago
For those who dont want to deal with handling the connection once created, here is a simple class that allows you to call any ftp function as if it were an extended method. It automatically puts the ftp connection into the first argument slot (as all ftp functions require).
This code is php 5.3+
<?php
class ftp{
public $conn;
public function __construct($url){
$this->conn = ftp_connect($url);
}
public function __call($func,$a){
if(strstr($func,'ftp_') !== false && function_exists($func)){
array_unshift($a,$this->conn);
return call_user_func_array($func,$a);
}else{
// replace with your own error handler.
die("$func is not a valid FTP function");
}
}
}
// Example
$ftp = new ftp('ftp.example.com');
$ftp->ftp_login('username','password');
var_dump($ftp->ftp_nlist());
?>
