0

estoy haciendo un sistema que me gestione archivos en PNG, JGP Y PDF, el sistema ya esta funcional, pero cuando intento subir un archivo JPG no funciona, con PNG si funciona igual que con PDF, pero no pasa nada al subir un archivo JPG, este es mi codigo.

<?php

include('conexion.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title = $con->real_escape_string(htmlentities($_POST['title']));
    $description = $con->real_escape_string(htmlentities($_POST['description']));

$file_name = $_FILES['file']['name'];

$new_name_file = null;

if ($file_name != '' || $file_name != null) {
    $file_type = $_FILES['file']['type'];
    list($type, $extension) = explode('/', $file_type);
    if ($extension == 'pdf' || $extension == 'png' || $extension == 'jpg') {
        $dir = 'files/';
        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }
        $file_tmp_name = $_FILES['file']['tmp_name'];
        //$new_name_file = 'files/' . date('Ymdhis') . '.' . $extension;
        $new_name_file = $dir . file_name($file_name) . '.' . $extension;
        if (copy($file_tmp_name, $new_name_file)) {
            
        }
    }
}

$ins = $con->query("INSERT INTO files(title,description,url) VALUES ('$title','$description','$new_name_file')");

if ($ins) {
    echo 'success';
} else {
    echo 'fail';
}
} else {
    echo 'fail';
}

1 respuesta 1

0

Agrégale tambien jpeg a la lista de extensiones, pues tambien es probable que a veces devuelva image/jpeg:

Cambia esto:

if ($extension == 'pdf' || $extension == 'png' || $extension == 'jpg') {

por esto:

if ($extension == 'pdf' || $extension == 'png' || $extension == 'jpg' || $extension == 'jpeg') {
2
  • Broo Muchas Gracias, ese era el problema, ya lo resolvi gracias a eso! el 29 dic. 2021 a las 1:09
  • Perfecto, me alegro :-)
    – masterguru
    el 29 dic. 2021 a las 1:10

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.