Ver Mensaje Individual
  #3 (permalink)  
Antiguo 27/05/2013, 16:17
Avatar de rulises
rulises
 
Fecha de Ingreso: enero-2008
Mensajes: 54
Antigüedad: 17 años, 1 mes
Puntos: 0
Respuesta: Bloquear subir imagen si la imagen existe

Explico completo el proceso.
Tengo una página que uso con un formulario para subir la imágen:

subir_imagen.php

<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "Administrador";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "../login/login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
<!DOCTYPE html PUBLIC ...
<script type="text/javascript">
function MM_popupMsg(msg) { //v1.0
alert(msg);
}
</script>
</head>

<body class="body">
<table width="100%" border="0" align="center" class="bordes">
<tr>
</tr>
</table>
<p>Seleccione un archivo.</p>
<form action="subir.php" method="POST" enctype="multipart/form-data">
<label for="Imagen">Imagen:</label>
<input type="file" name="Imagen" id="Imagen" />
<input type="submit" name="subir" value="Subir"/>
</form>
</table>
<p>&nbsp;</p>
</body>
</html>

ese formulario me lleva a la página subir.php:

<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "Administrador";
$MM_donotCheckaccess = "false";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;

// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}

$MM_restrictGoTo = "../login/login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>

<!DOCTYPE html PUBLIC ...
<meta http-equiv="Pragma" CONTENT="no-cache">
<script type="text/javascript">
function MM_popupMsg(msg) { //v1.0
alert(msg);
}
</script>
<meta http-equiv="Refresh" content="5;URL=nueva_entrada.php" />
</head>

<body class="body">
<table width="100%" border="0" align="center" class="bordes">
<tr>
</tr>
<tr>
</tr>
</table>
<p class="encabezado2">
</p>
<br />
</p>
<p><?php
//conexion a la base de datos
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("francisco") or die(mysql_error()) ;

//comprobamos si ha ocurrido un error.
if ($_FILES["Imagen"]["error"] > 0){
echo "ha ocurrido un error";
} else {
//ahora vamos a verificar si el tipo de archivo es un tipo de imagen permitido.
//y que el tamano del archivo no exceda los 100kb
$permitidos = array("image/jpg", "image/jpeg", "image/gif", "image/png", "image/bmp");
$limite_kb = 1800;

if (in_array($_FILES['Imagen']['type'], $permitidos) && $_FILES['Imagen']['size'] <= $limite_kb * 2048){
//esta es la ruta donde copiaremos la imagen
//recuerden que deben crear un directorio con este mismo nombre
//en el mismo lugar donde se encuentra el archivo subir.php
$ruta = "../Media/Imagenes/" . $_FILES['Imagen']['name'];
//comprobamos si este archivo existe para no volverlo a copiar.
//pero si quieren pueden obviar esto si no es necesario.
//o pueden darle otro nombre para que no sobreescriba el actual.
if (!file_exists($ruta)){
//aqui movemos el archivo desde la ruta temporal a nuestra ruta
//usamos la variable $resultado para almacenar el resultado del proceso de mover el archivo
//almacenara true o false
$resultado = @move_uploaded_file($_FILES["Imagen"]["tmp_name"], $ruta);
if ($resultado){
$nombre = "Media/Imagenes/".$_FILES['Imagen']['name'];
@mysql_query("INSERT INTO imagenes (Imagen) VALUES ('$nombre')") ;
echo "La imágen ha sido cargada correctamente";
} else {
echo "ocurrio un error al cargar la imágen.";
}
} else {
echo $_FILES['Imagen']['name'] . ", este archivo existe";
}
} else {
echo "archivo no permitido, es tipo de archivo prohibido o excede el tamano de $limite_kb Kilobytes";
}
}

?>&nbsp;</p>

</tr>
<tr
</tr>
</table>
<p>&nbsp;</p>
</body>
</html>

y al cargar la imágen me lleva a otra página, nueva entrada.php:

lo pongo en otro mensaje porque me dice que excedo el limite de caracteres
sigue abajo