PHP - Noob intenta hacer un cliente ftp con php y sale mal

 
Vista:
sin imagen de perfil

Noob intenta hacer un cliente ftp con php y sale mal

Publicado por Aitor (2 intervenciones) el 02/03/2023 17:03:42
Estoy intentando hacer un cliente ftp con php y creando usuarios en una base de datos. Consigo que se conecte y me muestra la ruta del directorio en el que está, pero no me lista los archivos ni me deja subir nada.

index.php =

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>FTP</title>
<meta 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>
Funciones FTP
</em></strong></font></p>
<p><font face="Verdana, Tahoma, Arial">
 
<?php
include_once "./config/config.php";
include_once "./funciones.php"; //Incluye el archivo de funciones
if(!empty($_POST["archivo"])){ //Comprueba si la variable "archivo" se ha definido
SubirArchivo($_POST["archivo"],basename($_POST["archivo"]));
//basename obtiene el nombre de archivo sin la ruta
unset($_POST["archivo"]); //Destruye la variable "archivo"
}
?>
<strong><font color="#000000" size="3">Subir Archivo</font></strong></font></p>
<hr />
 
<!--Formulario para elejir el archivo a subir -->
<form action="" method="post" name="form_ftp" id="form_ftp" enctype="multipart/form-data">
<p><font size="2" face="Verdana, Tahoma, Arial"> Elegir archivo :
<input name="archivo" type="file" id="archivo" />
<input name="Submit" type="submit" value="Subir Archivo" />
</font><font size="2" face="Verdana, Tahoma, Arial"> </font> </p>
</form>
 
<hr />
<p><font face="Verdana, Tahoma, Arial"><strong><font color="#000000" size="3">
Lista de Archivos
</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>
<?php
$id_ftp=ConectarFTP(); //Obtiene un manejador y se conecta al Servidor FTP
$ruta=ObtenerRuta(); //Obtiene la ruta actual en el Servidor FTP
echo "<b>El directorio actual es: </b> ".$ruta;
$lista=ftp_nlist($id_ftp,$ruta); //Devuelve un array con los nombres de ficheros
$lista=array_reverse($lista); //Invierte orden del array (ordena array)
while ($item=array_pop($lista)) //Se leen todos los ficheros y directorios del directorio
{
$tamano=number_format(((ftp_size($id_ftp,$item))/1024),2)." Kb";
//Obtiene tamaño de archivo y lo pasa a KB
if($tamano=="-0.00 Kb") // Si es -0.00 Kb se refiere a un directorio
{
$item="<i>".$item."</i>";
$tamano=" ";
$fecha=" ";
}else{
$fecha=date("d/m/y h:i:s", ftp_mdtm($id_ftp,$item));
//Filemtime obtiene la fecha de modificacion del fichero; y date le da el formato de salida
}
?>
 
<tr>
<td><font size="2" face="Verdana, Tahoma, Arial"><?php echo $item ?></font></td>
<td align="right"><font size="2" face="Verdana, Tahoma, Arial"><?php echo $tamano ?></font></td>
<td align="right"><font size="2" face="Verdana, Tahoma, Arial"><?php echo $fecha ?></font></td>
</tr>
<?php } ?>
</table>
</body>
</html>

funciones.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
session_start();
include_once "./config/config.php";
define("PASV",false); //Activa modo pasivo
$nombre = $_SESSION['nombre'];
$conex = mysqli_connect($host, $user, $password, $db, $port);
$query = "SELECT nombre, password, rol FROM usuarios WHERE nombre='$nombre'";
$resultado = mysqli_query($conex, $query);
$ftpHost = 'localhost';
while ($fila = mysqli_fetch_assoc($resultado)) {
$rol = $fila["rol"];
$ftpUsername = $fila["nombre"];
$ftpPassword = $fila["password"];
}
if ($rol == "admin"){
$ftpUsername = "admin";
$ftpPassword = "censored";
}
if ($rol == "rrhh"){
$ftpUsername = "rrhh";
$ftpPassword = "censored";
}
if ($rol == "anon"){
$ftpUsername = "whoever";
$ftpPassword = "censored";
}
if ($rol == "designer"){
$ftpUsername = "designer";
$ftpPassword = "censored";
}
mysqli_close($conex);
 
# FUNCIONES
 
//login
function ConectarFTP(){
global $ftpHost, $id_ftp, $ftpUsername, $ftpPassword;
//Permite conectarse al Servidor FTP
$id_ftp=ftp_connect($ftpHost); //Obtiene un manejador del Servidor FTP
ftp_login($id_ftp,$ftpUsername,$ftpPassword); //Se loguea al Servidor FTP
ftp_pasv($id_ftp,PASV); //Establece el modo de conexión
return $id_ftp; //Devuelve el manejador a la función
}
 
 
//subir archivo
function SubirArchivo($archivo_local,$archivo_remoto){
$ftpHost = 'localhost';
$id_ftp=ftp_connect($ftpHost);
//Sube archivo de la maquina Cliente al Servidor (Comando PUT)
 
ftp_put($id_ftp,$archivo_remoto,$archivo_local,FTP_BINARY);
//Sube un archivo al Servidor FTP en modo Binario
ftp_close($id_ftp); //Cierra la conexion FTP
}
 
 
function ObtenerRuta(){
//Obriene ruta del directorio del Servidor FTP (Comando PWD)
$id_ftp=ConectarFTP(); //Obtiene un manejador y se conecta al Servidor FTP
$Directorio=ftp_pwd($id_ftp); //Devuelve ruta actual p.e. "/home/willy"
ftp_quit($id_ftp); //Cierra la conexion FTP
return $Directorio; //Devuelve la ruta a la función
}
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder