Académique Documents
Professionnel Documents
Culture Documents
FACULTAD DE INGENIERIA
INTRODUCCION COMERCIO ELECTRONICO
Gestión de Archivos
A continuación se muestran algunas funciones elementales para la gestión de archivos.
Existen distintos modos de apertura que nos permiten definir las acciones que podemos realizar sobre el
archivo. Aquí os mostramos los diferentes modos que, como veréis, son de lo más variado:
1. Subir archivos haciendo uso de formularios HTML y la variable global $_FILES de PHP
mensaje.html
<html>
<head><title>Mensaje de correoc</title></head>
<body bgcolor="#3399FF">
<form action=recibido.php method=POST enctype="multipart/form-data">
<table align=center>
<tr>
<td colspan=2><hl align=center>Correo electrónico</hl><hr></td>
</tr>
<tr>
<td>A:</td>
<td><input type=text name=destino size=40 maxlenght=40></td>
<tr>
<tr>
<td>Mensaje:</td>
<td><textarea name=mensaje cols=30 rows=6></textarea></td>
</tr>
<tr>
<td>Archivo adjunto:</td>
<td><input type="file" name="adjunto" title="Seleccione el archivo a enviar">
</tr>
<tr>
<td align=center colspan=2>
<input type=submit value=Enviar>
<input type=reset value=Reestablecer>
</td>
</tr>
</table>
</form>
</body>
</hmtl>
recibido.php
<html>
<head><title>Verificar las características del archivo recibido</title></head>
<body text=white>
<table align=center border=1 bgcolor="#553399">
<tr>
<td colspan=2> <hl align=center>Archivo recibido</hl> </td>
</tr>
<tr>
<td>Nombre del archivo recibido:</td>
<td>
<?php
echo $_FILES['adjunto']['name'];
?>
</td>
</tr>
<tr>
<td>Tamaño del archivo:</td>
<td>
<?php
echo $_FILES['adjunto']['size']. "bytes";
?>
</td>
</tr>
<tr>
<td>Tipo de archivo (MIME):</td>
<td>
<?php
echo $_FILES['adjunto']['type'];
?>
</td>
</tr>
<tr>
<td>Nombre temporal en el servidor:</td>
<td>
<?php
echo $_FILES['adjunto']['tmp_name'];
?>
</td>
</tr>
<tr>
<td>Direccion actual:</td>
<td>
<?php
$ruta_destino = './'; //guarda en el mismo directorio donde esta el archivo PHP
move_uploaded_file($_FILES['adjunto']['tmp_name'],$ruta_destino.
$_FILES['adjunto']['name']);
echo $ruta_destino.$_FILES['adjunto']['name'];
?>
</td>
</tr>
</table>
</body>
</html>
En el primer script, se crea el archivo “users.txt” donde es almacenada la cuanta del usuario con
su contraseña de manera cifrada. Todo esto con funciones de manejo de archivos de PHP
iniciosesion.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Autenticación de usuarios</title></head>
<body>
<?php
if(isset($_POST['user']) && (isset($_POST['pass']))){
$pwfile = fopen("users.txt", "a");
fputs($pwfile, $_POST['user'] . ":" . crypt($_POST['pass'], "pw")."\n");
fclose($pwfile);
?>
Usuario (<?php echo htmlspecialchars($_POST['user']) . ":" .
crypt($_POST['pass']."pw") ?>) agregado
<?php } ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<table>
<tr>
<td><label for="user">Usuario: </label></td>
<td><input type="text" name="user" size="25" maxlength="25"></td>
</tr>
<tr>
<td><label for="pass">Contraseña: </label></td>
<td><input type="password" name="pass" size="25" maxlength="25"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Encrypt!"></td>
</tr>
</table>
</form>
</body>
</html>
comprobarusuario.php
<?php
if(isset($_SERVER['PHP_AUTH_USER'])){
$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];
}
else if(isset($_SERVER['HTTP_AUTHORIZATION'])){
if(substr($_SERVER['HTTP_AUTHORIZATION'],0,5) == "Basic"){
$userpass = split(":",
base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
$user = $userpass[0];
$pass = $userpass[1];
}
}
$auth = false;
$pwfile = fopen("users.txt", "r");
while(!feof($pwfile)){
$data = split(":", rtrim(fgets($pwfile, 1024)));
if($user == $data[0] && crypt($pass, "pw") == $data[1]){
$auth = true;
break;
}
}
fclose($pwfile);
if(!$auth){
header("WWW-Authenticate: Basic realm=\"PHP 5 Unleashed Protected Area\"");
header("HTTP/1.0 401 Unauthorized");
$msgden = "<h2 style=\"font-family:Impact;font-size:15pt;color:Red;\">";
$msgden .= "No tienes acceso a este sitio</h2>";
echo $msgden;
}
else{
echo "<h3>Bienvenido, $user!</h3>";
}
?>
Tarea.
Modifique el ejemplo dos agregando el código necesario para llevar el historial de accesos exitosos y
fallidos, almacenándolos en un archivo de texto con el nombre de “historial.txt”, especificando el nombre
del usuario y si el acceso fue exitoso o fallido