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
- Introduction
- Installation/Configuration
- Constantes pré-définies
- Exemples
- Fonctions FTP
- ftp_alloc — Alloue de l'espace pour un téléchargement de fichier
- ftp_cdup — Change de dossier et passe au dossier parent
- ftp_chdir — Modifie le dossier FTP courant
- ftp_chmod — Modifie les droits d'un fichier via FTP
- ftp_close — Ferme une connexion FTP
- ftp_connect — Ouvre une connexion FTP
- ftp_delete — Efface un fichier sur un serveur FTP
- ftp_exec — Exécute une commande sur un serveur FTP
- ftp_fget — Télécharge un fichier via FTP dans un fichier local
- ftp_fput — Charge un fichier sur un serveur FTP
- ftp_get_option — Lit différentes options pour la connexion FTP courante
- ftp_get — Télécharge un fichier depuis un serveur FTP
- ftp_login — Identification sur un serveur FTP
- ftp_mdtm — Retourne la date de dernière modification d'un fichier sur un serveur FTP
- ftp_mkdir — Crée un dossier sur un serveur FTP
- ftp_nb_continue — Reprend le téléchargement d'un fichier (non bloquant)
- ftp_nb_fget — Lit un fichier sur un serveur FTP, et l'écrit dans un fichier (non bloquant)
- ftp_nb_fput — Écrit un fichier sur un serveur FTP, et le lit depuis un fichier (non bloquant)
- ftp_nb_get — Lit un fichier sur un serveur FTP, et l'écrit dans un fichier (non bloquant)
- ftp_nb_put — Envoie un fichier sur un serveur FTP (non-bloquant)
- ftp_nlist — Retourne la liste des fichiers d'un dossier
- ftp_pasv — Active ou désactive le mode passif
- ftp_put — Charge un fichier sur un serveur FTP
- ftp_pwd — Retourne le nom du dossier courant
- ftp_quit — Alias de ftp_close
- ftp_raw — Envoie une commande FTP brute
- ftp_rawlist — Fait une liste détaillée des fichiers d'un dossier
- ftp_rename — Renomme un fichier sur un serveur FTP
- ftp_rmdir — Efface un dossier FTP
- ftp_set_option — Modifie les options de la connexion FTP
- ftp_site — Exécute la commande SITE sur un serveur FTP
- ftp_size — Retourne la taille d'un fichier
- ftp_ssl_connect — Ouvre une connexion FTP sécurisée avec SSL
- ftp_systype — Retourne un identifiant de type de serveur FTP
asifkhandk at gmail dot com ¶
3 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());
?>
