To make a secure form and that the information you upload to your server does not put at risk.
You can validate the extensions of the files that upload to your site using the global variable $_FILES["codigo"]
.
The error codes that can be returned $_FILES[‘mi_archivo’][‘error’]
are:
The selected file exceeds the maximum size allowed in php.ini (we can know the maximum size allowed using the function ini_get(‘upload_max_filesize’)
).
The uploaded file exceeds the MAX_FILE_SIZE
directive, if it was specified in the form.
The uploaded file was only partially loaded.
No files have been uploaded.
Temporary storage directory is missing.
Unable to write file (possible problem related to write permissions).
With this code you can check what kind of files you allow to upload to your server:
<?php
//tomo el valor de un elemento de tipo texto del formulario
$cadenatexto = $_POST["cadenatexto"];
echo "Escribió en el campo de texto: " . $cadenatexto . "<br><br>";
//datos del arhivo
$nombre_archivo = $_FILES['userfile']['name'];
$tipo_archivo = $_FILES['userfile']['type'];
$tamano_archivo = $_FILES['userfile']['size'];
//compruebo si las características del archivo son las que deseo
if (!((strpos($tipo_archivo, "gif") || strpos($tipo_archivo, "jpeg")) && ($tamano_archivo < 100000))) {
echo "La extensión o el tamaño de los archivos no es correcta. <br><br><table><tr><td><li>Se permiten archivos .gif o .jpg<br><li>se permiten archivos de 100 Kb máximo.</td></tr></table>";
}else{
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $nombre_archivo)){
echo "El archivo ha sido cargado correctamente.";
}else{
echo "Ocurrió algún error al subir el fichero. No pudo guardarse.";
}
}
?>
I would also recommend you use a captcha on your form.
An additional recommendation to forget about this kind of problems uses a development framework such as Symfony , for example, that emphasize security, nothing is 100% safe but it will help you a lot.
Greetings