|
The functions are explained to realize files transference, using the protocol FTP, from pages PHP. Example of code to learn to raise files to the servant.
Constantly we are lowering Internet files: A programilla that someone recommends to us, the photo of the fashionable model or the outstanding figures MP3's. Without realizing one or another way we make use of the protocol FTP (File Transfer Prococol) to lower files from a Servant. We discharge more than we rise... in other words "We take more than we give". Be article tries to tell how to rise - or to allow his users to rise they should (reach port) - files to the Servant using the functions FTP included in the PHP. Remember the old saying that quotes: "More it costs to give than to receive".
WHAT IS THE FTP?
Initials of File Transfer Protocol or Protocol of Transference of Files. As its own name indicates it, it is a protocol (belonging to TCP/IP) that takes charge of the files transference between computers connected in network. After there bases on the architecture Client / Servant, the FTP makes use of two basic components:
-
A client FTP. Which is in charge of connecting a servant FTP to unload or to raise files.
-
A servant FTP. He is in charge of processing the requests of the clients FTP, allowing them to discharge or raise files from him.
To get connected to a servant FTP, and this way to be able to realize consultations in him, we will need the following information:
-
Name of the Servant. It is the IP or Name of the Servant FTP to which we have to get connected, such as: 65.134.10.5 or ftp.billysite.net
- Port. Number of the port of the servant. For defect it is 21.
-
User's account. It is the name of the account of usario that has been assigned to us. It is necessary to make sure that it is provided with the necessary permissions to raise or to lower files. Of not having a user's account it is possible to gain access like anonymous user using the user's name anonymous.
-
Access key. It is our user's account password. Of gaining access like anonymous user we will place like key our mail and like courteousness.
Once connected to the servant FTP we will be able to make use of his commands to realize the tasks that better we create suitably. This article does not try to outline to a great extent the FTP topic, after this is not the fundamental target of the same one. For més I recommend information about this aspect to him to check the árticulo of Michael Calore: "The ABC of the transference of files for Internet", available in the WebMonkey web site.
FUNCTIONS FTP IN PHP.
PHP makes use of functions FTP to gain access even web servant, like client. Next we will show the basic functions to be used in the script, as well as a brief description of the same. If he wants major detail of these and other functions FTP I advise him to consult the official papers of the PHP, available in: http://www.php.net/docs.php.
|
Function
|
Syntax
|
Description
|
| ftp_connect |
int ftp_connect (string host [int port])
host: Name or Servant's IP FTP.
port: Port, for defect 21. |
It establishes a connection FTP to the stated host. |
| ftp_login |
int ftp_login (int ftp_stream, string username, string password)
ftp_stream: Manejador FTP obtained with ftp_connect.
username: User's name.
password: constraseña of user. |
The meeting begins in a connection FTP.
|
| ftp_pasv |
int ftp_pasv (int ftp_stream, int pasv)
ftp_stream: Manejador FTP obtained with ftp_connect.
pasv: If active TRUE is the passive way, if it is FALSE it deactivates it. |
It activates or deactivates the passive way. In passive way, the information connections are initiated by the client, instead of being initiated by the servant. |
| ftp_pwd |
int ftp_pwd (int ftp_stream)
ftp_stream: Manejador FTP obtained with ftp_connect. |
He returns the name of the current directory. |
| ftp_put |
int ftp_put (int ftp_stream, string remote_file, string local_file, int mode)
ftp_stream: Manejador FTP obtained with ftp_connect.
remote_file: Name with which one will keep the file in the Servant FTP.
local_file: Local file that client is in the machine.
mode: Transference way, it can be FTP_ASCII or FTP_BINARY. |
It raises a file to the Servant FTP. |
| ftp_nlist |
int ftp_nlist (int ftp_stream, string directory)
ftp_stream: Manejador FTP obtained with ftp_connect.
directory: Route of the directory to be listed. |
He returns a list of files of the given directory.
|
| ftp_size |
int ftp_size (int ftp_stream, string remote_file)
ftp_stream: Manejador FTP obtained with ftp_connect.
remote_file: Name of the file in the Servant FTP. |
He returns the size of the stated file. Not all the servants support this characteristic. |
| ftp_mdtm |
int ftp_mdtm (int ftp_stream, string remote_file)
ftp_stream: Manejador FTP obtained with ftp_connect.
remote_file: Name of the file in the Servant FTP. |
He returns the date of last modification of the stated file. Not all the servants support this characteristic |
| ftp_quit |
int ftp_quit (int ftp_stream)
ftp_stream: Manejador FTP obtained with ftp_connect. |
It closes a connection FTP |
| It notices: He must make sure that the functions should be qualified ftp in the configuration of the version of PHP that it possesses and of having the necessary permissions in his account FTP to raise and to lower files. |
CODE SOURCE.
/inc/ftpfunc.php.
Script that will contain the constants and functions to be used in index.php. In this script it will have to modify the values of the constants to it fit to his needs. The function ConectarFTP will allow him to get connected to the Servant FTP; the function SubirArchivo has the task of raising a file to the Servant; and finally, the function ObtenerRuta will grant him the route of the current directory at which the Servant is employed.
<?
# FUNCTIONS FTP # CONSTANTS
# Change this information into those of his Servant FTP
"SERVER" defines ("localhost");//IP or Name of the Servant
"PORT" defines (21);//Port
"USER" defines ("willy");//User's Name
"PASSWORD" defines ("12345");//access Password
"PASV" defines (true);//It Activates passive way
# FUNCTIONS
function ConectarFTP () {
//It allows to get connected to the Servant FTP
$id_ftp=ftp_connect (SERVER, PORT);//It Obtains a manejador of the Servant FTP
ftp_login ($id_ftp, USER, PASSWORD);//loguea to the Servant FTP
ftp_pasv ($id_ftp, WAY);//It Establishes the connection way
return $id_ftp;//He Returns the manejador to the function
}
function SubirArchivo ($archivo_local, $archivo_remoto) {
//Client raises file of the machine to the Servant (I command PUTT)
$id_ftp=ConectarFTP ();//It Obtains a manejador and gets connected to the Servant FTP
ftp_put ($id_ftp,$archivo_remoto,$archivo_local,FTP_BINARY);
//It raises a file to the Servant FTP in Binary way
ftp_quit ($id_ftp);//It Closes the connection FTP
}
function ObtenerRuta () {
//Obriene route of the directory of the Servant FTP (Command PWD)
$id_ftp=ConectarFTP ();//It Obtains a manejador and gets connected to the Servant FTP
$Directorio=ftp_pwd ($id_ftp);//p.e Returns current route. "/home/willy"
ftp_quit ($id_ftp);//It Closes the connection FTP
return $Directorio;//He Returns the route to the function
}
?>
|
index.php.
Script that contains a form (form_ftp) that allows us to look for a file and to raise it to the Servant FTP, also shows us a list of the directories and files of the same one.

|
<? php do I begin "<? xml version = "1.0" encoding = "iso-8859-1"?"."> ";?>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//IN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>::. Functions FTP.:: </title>
<goal http-equiv = "Content-Type" content = "text/html; charset=iso-8859-1"/>
</head>
<body>
<p align = "center"> <font size = "5" face = "Verdana, Tahoma, Arial"> <strong> <em>
Functions FTP
</em> </strong> </font> </p>
<p> <font face = "Verdana, Tahoma, Arial">
<?
include ('inc/ftpfunc.php');//It Includes the functions file
if (! empty ($_POST [I "file"])) {//He verifies if the variable "filed" has been defined
SubirArchivo ($_POST ["file], basename ($_POST ["file]));
//basename it obtains the name of file without the route
unset ($_POST ["file]);//It Destroys the variable I "file"
}
?>
<strong> <font color = "#000000" size = "3"> to Raise File </font> </strong> </font> </p>
<hr/>
<! - Form for elejir the file to be raised->
<form action = "" method = "post" yam = "form_ftp" go = "form_ftp">
<p> <font size = "2" face = "Verdana, Tahoma, Arial"> to Choose file:
<unputt yam = "file" type = "file" go = "file"/>
<unputt yam = "Submit" type = "submit" values = "to Raise File"/>
</font> <font size = "2" face = "Verdana, Tahoma, Arial"> </font> </p>
</form>
<hr/>
<p> <font face = "Verdana, Tahoma, Arial"> <strong> <font color = "#000000" size = "3">
List of Files
</font> </strong> </font> </p>
<table width = "69 %" border = "1" cellspacing = "0" cellpadding = "0">
<tr>
<td width = "48 %"> <div align = "center"> <font size = "2" face = "Verdana, Tahoma, Arial" ><strong>Nombre</strong></font></div></td>
<td width = "22 %"> <div align = "center"> <font size = "2" face = "Verdana, Tahoma, Arial" ><strong>Tamaño</strong></font></div></td>
<td width = "30 %"> <div align = "center"> <font size = "2" face = "Verdana, Tahoma, Arial"> <strong> Fec.
Modificación</strong></font></div></td>
</tr>
<?
$id_ftp=ConectarFTP ();//It Obtains a manejador and gets connected to the Servant FTP
$ruta=ObtenerRuta ();//It Obtains the current route in the Servant FTP
I begin "<b> The current directory is: </b>".$ruta;
$lista=ftp_nlist ($id_ftp, $ruta);//He Returns an array with the names of files
$lista=array_reverse ($lista);//He Invests order of the array (it orders array)
while ($item=array_pop ($lista))//there are read all the files and directories of the directory
{
$tamano=number_format (((ftp_size ($id_ftp, $item))/1024), 2)." Kb";
//It obtains file size and spends it to KB
if ($tamano == "-0.00 Kb")//If it is-0.00 Kb it refers to a directory
{
$item =" <i> ".$item." </i> ";
$tamano = " ";
$fecha = " ";
}else {
$fecha=date ("d / m / and h:i:s", ftp_mdtm ($id_ftp, $item));
//Filemtime obtains the date of modification of the file; and date it gives him the exit format
}
?>
<tr>
<td> <font size = "2" face = "Verdana, Tahoma, Arial"> <? do I begin $item?> </font> </td>
<td align = "right"> <font size = "2" face = "Verdana, Tahoma, Arial"> <? do I begin $tamano?> </font> </td>
<td align = "right"> <font size = "2" face = "Verdana, Tahoma, Arial"> <? do I begin $fecha?> </font> </td>
</tr>
<?}?>
</table>
</body>
</html>
|
Very well, I hope that this one should contribute to the Web Community it has been useful to them, any doubt or suggestion do not doubt in hacermesla to come. Greetings.
William Wong Garay
http://billysite.net |