I want to save a photo to the database but I am having a very strange problem. When I press the button to save the image in the database it is being saved in the folder of my server "img-profile" it is supposed that it is passing the filter, but the name and extension are not saved in the DB and I have created one var_dump
to see if something is coming to me through $_FILES[] and it says that nothing is coming to me but it is passing the filter that was programmed, because the image arrives correctly in the folder img-profile
and is saved with its name and extension but no It is saved in the DB because var_dump
nothing is coming through it.
HTML5 form:
<!-- Modal Structure -->
<div id="photo-complete" class="modal">
<div class="modal-content">
<h4 class="flow-text pink-text center">Coloca una foto de perfil</h4>
<form action="./backend/photo-profile.php" method="POST" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn btn-color">
<span><i class="material-icons">photo_camera</i></span>
<input type="file" name="photo" required>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" name="photo" type="text" placeholder="Selecciona una foto de perfil">
</div>
</div>
<button class="btn waves-effect right btn-color" type="submit" name="submit">enviar
<i class="material-icons left">send</i>
</button><br><br><br>
</form>
</div>
</div>
PHP logic:
<?php session_start();
require('../connection/connection.php');
$email = $_SESSION['email'];
require('../user/user.php');
$errors = '';
if(isset($_FILES['photo'])){
$photo = $_FILES['photo'];
var_dump($photo);
// Verificar que nos llegue la imagen
if(empty($photo)){
$errors = 'Selecciona una imagen';
}
$photoName = $photo['name'];
$photoType = $photo['type'];
// Asegurarnos que la imagen contenga un formato de img
if($photoType == "image/jpg" || $photoType == "image/png" || $photoType == "image/jpeg" || $photoType == "image/git" || $photoType == "image/gif" || $photoType == ""){
// Verificar el peso de la imagen
if($photo['size'] >= $maxSize) {
$errors .= 'La imagen pesa mucho, por favor solo 2MB';
}
if(!is_dir('../img-profile')){
mkdir('../img-profile', 0777);
}
// Movemos la img a la carpeta photo
move_uploaded_file($photo['tmp_name'], '../img-profile/'.$photoName);
}else {
$errors .= "Lo siento, no aceptamos esta extension $photoName";
}
// No hay problemas y pasa por nuestro filtro, dejalo registrar el producto
if($errors == ''){
$statement = $conexion->prepare("UPDATE users SET photo_profile = '$photoName' WHERE id_user = $id_user"
);
$statement->execute(array(
':photoName' => $photoName,
':id_user' => $id_user
));
header('Location: ./photo-cover');
}else{
header('Location: ./photo-profile');
}
var_dump($errors);
}else{
echo 'No llega nada';
}
When not defined
$maxSize
, the file size is evaluated to be greater than zero and the error is added to$errors
.When not fulfilled
$errors == ''
, the same php is reloaded withheader('Location: ./photo-profile')
but now without files, resulting in the message "Nothing arrives".