Como validar para subir determinados archivos al servidor en PHP
Hola.
Si alguien me puede ayudar se lo agradecere mucho.
Como puedo validar este script en PHp para que solo se puedan subir determinados archivos (Por ejemplo .jpg) y que me mande un mensaje de error si no corresponde al tipo de archivo, y es posible limitar el tamaño del archivo...?
Gracias....
-----------------------------------------------------------------------
<html>
<head>
<title>Documento sin título</title>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center"><img src="Top.png" width="747" height="112"></div></td>
</tr>
<tr>
<td><div align="center"><img src="xbeatPanel.png" width="809" height="603">
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" value="Buscar" />
<input type="submit" name="submit" value="Subir Archivo" />
</form>
<?
$destino = 'Singles';
copy($_FILES['file']['tmp_name'], $destino.'/'.$_FILES['file']['name'])
?>
</div></td>
</tr>
<tr>
<td><div align="center"></div></td>
</tr>
</table>
</body>
</html>
Si alguien me puede ayudar se lo agradecere mucho.
Como puedo validar este script en PHp para que solo se puedan subir determinados archivos (Por ejemplo .jpg) y que me mande un mensaje de error si no corresponde al tipo de archivo, y es posible limitar el tamaño del archivo...?
Gracias....
-----------------------------------------------------------------------
<html>
<head>
<title>Documento sin título</title>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center"><img src="Top.png" width="747" height="112"></div></td>
</tr>
<tr>
<td><div align="center"><img src="xbeatPanel.png" width="809" height="603">
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" value="Buscar" />
<input type="submit" name="submit" value="Subir Archivo" />
</form>
<?
$destino = 'Singles';
copy($_FILES['file']['tmp_name'], $destino.'/'.$_FILES['file']['name'])
?>
</div></td>
</tr>
<tr>
<td><div align="center"></div></td>
</tr>
</table>
</body>
</html>
1ª PARTE
La función getimagesize($path.$archivo) te devuelve un array en el que los elementos dan:
[0] -> Ancho de la imagen en px
[1] -> Alto de la imagen en px
[2] -> El tipo de imagen (1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP.... etc. En el manual las tienes todas.)
[3] -> Te dá la cadena height="yyy" width="xxx" para meterla directamente en la etiqueta <IMG>
$tipoarchivo=getimagesize("$path$pathext$file");
if($tipoarchivo[2] != 2) {
// no es un jpg
} else {
//si que lo es
}
2ª PARTE
Co esto te aseguras que no este vacÃo y que tenga el tamaño adecuado:
if($HTTP_POST_FILES['uploadedfile']['size'] > 0 && $HTTP_POST_FILES['uploadedfile']['size'] < $MaxFileSize)
Espero que te sirva
Salu2
Paco
La función getimagesize($path.$archivo) te devuelve un array en el que los elementos dan:
[0] -> Ancho de la imagen en px
[1] -> Alto de la imagen en px
[2] -> El tipo de imagen (1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP.... etc. En el manual las tienes todas.)
[3] -> Te dá la cadena height="yyy" width="xxx" para meterla directamente en la etiqueta <IMG>
$tipoarchivo=getimagesize("$path$pathext$file");
if($tipoarchivo[2] != 2) {
// no es un jpg
} else {
//si que lo es
}
2ª PARTE
Co esto te aseguras que no este vacÃo y que tenga el tamaño adecuado:
if($HTTP_POST_FILES['uploadedfile']['size'] > 0 && $HTTP_POST_FILES['uploadedfile']['size'] < $MaxFileSize)
Espero que te sirva
Salu2
Paco
