I would like to know what this error is due to, I have already put all the validations correctly but apparently it keeps telling me that there is an error in my variable $archivo
.
HTML
<form action="reg.php" method="post">
<div class="inicio">
<h1>Registro</h1>
<input type="text" placeholder="Nombre" name="_name">
<input type="text" placeholder="Apellido Paterno" name="_lastName">
<input type="text" placeholder="Apellido Materno" name="_secondName">
<input type="text" placeholder="Correo" name="_email">
<input type="text" placeholder="Contraseña" name="_password">
<input type="text" placeholder="Repetir Contraseña" name="_repPassword">
<input type="file" class="btn-img" name="up-image" id="up-image">
<button class="btn-main">Registrar Usuario</button>
<span><a href="./index.php">Cancelar</a></span>
</div>
</form>
PHP
if(isset($_POST['_name'])) {
$name = $_POST['_name'];
$lastName = $_POST['_lastName'];
$secondName = $_POST['_secondName'];
$email = $_POST['_email'];
$password = $_POST['_password'];
$repPassword = $_POST['_repPassword'];
if ($_FILES['up-image']['error']>0) {
echo "Error al cargar el archivo";
} else {
$date = new DateTime();
$newId = $date->format('H-i-s');
$ruta = 'files/'.$newId.'/';
$archivo = $ruta.$_FILES['up-image']['name'];
if (!file_exists($ruta)) {
mkdir($ruta);
}
if(!file_exists($archivo)){
$resultado = @move_uploaded_file($_FILES['up-image']['tmp_name'], $archivo);
if ($resultado) {
echo ' Archivo guardado <br>';
} else {
echo ' Error al guardar el archivo <br>';
}
} else {
echo 'El archivo ya existe <br>';
}
}
echo 'Los datos fueron almacenados correctamente' . '<br>';
}
echo $name.'<br>'.$lastName.'<br>'.$secondName.'<br>'.$email.'<br>'.$password.'<br>'.$repPassword;
Mistake
Notice: Undefined index: up-image in C:\xampp\htdocs\FormPHP\reg.php on line 11
Notice: Undefined index: up-image in C:\xampp\htdocs\FormPHP\reg.php on line 17
To send files using
form
, you must set the attributeenctype
equal tomultipart/form-data
, which according to the documentation:Example:
Suggestion:
One validation you could add is to check that the variable is defined: