0

I'm trying to upload pdf files to the server but for some reason everything that I've tried doesn't work. I've changed permissions on folders, files..(read + write) I've also changed max file size in php.ini All the other files work fine except pdf. Can someone please help me figure this out? Thank you in advance.

   <?php
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["archivoSubir"]["name"]);
  $uploadOk = 1;
  $tipoFichero = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Comprobar si el archivo actual es una imagen o no
  if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["archivoSubir"]["tmp_name"]);
    if($check !== false) {
      echo "El archivo es una imagen - " . $check["mime"] . ".";
      $uploadOk = 1;
    } else {
      $uploadOk = 0;
    }
  }
  // Limitar formato de archivos
  if($tipoFichero != "jpg" && $tipoFichero != "png" && $tipoFichero != "jpeg"
  && $tipoFichero != "gif" && $tipoFichero != "application/pdf") {
    echo 'Lo siento, solo archivos JPG, JPEG, PNG, GIF & PDF están permitidos.';
    print_r($_FILES);
    $uploadOk = 0;
  }
  // Limitar tamaño de fichero
  if ($_FILES["archivoSubir"]["size"] > 5000000) {
    echo 'El archivo que intentas subir es demasiado grande.';
    $uploadOk = 0;
  }
  // Comprobamos si el archivo ya existe
  if (file_exists($target_file)) {
    echo 'El archivo ya existe.';
    $uploadOk = 0;
  }

  // Comprobar si ha habido un error en la subida
  if ($uploadOk == 0) {
    echo 'El archivo no se ha podido subir.';
  // Si todo está bien, subimos el archivo
  } else {
    if (move_uploaded_file($_FILES["archivoSubir"]["tmp_name"], $target_file)) {
      echo 'El archivo '. htmlspecialchars( basename( $_FILES["archivoSubir"]["name"])). ' ha sido subido.';
      header( "refresh:2;url=index.php" );
    } else {
      echo 'Ha habido un error al subir el archivo.';
    }
  }
?>
  • sorry to say, but arent you checking for extension? ... so extension in this case would be in your case `$tipoFichero != "pdf")` rather than `application/pdf.` What does, when you try uploading a pdf. `$tipoFichero` return even? Debugging 101 :) – Dorvalla Jan 28 '22 at 19:51
  • You're right. But even then the file doesn't upload to the server. It says the file couldn't be uploaded (echo 'Ha habido un error al subir el archivo.') and when I do a print_r ($files) error[0] shows up but then again, doesnt error [0] mean that everything is fine? – josefdzbeta Jan 28 '22 at 19:53
  • how does your html look like then with the upload. Have you used the `accept` attribute in your input? – Dorvalla Jan 28 '22 at 19:54
  • I've never heard of the accept attribute, let me look it up real quick – josefdzbeta Jan 28 '22 at 19:56
  • https://stackoverflow.com/questions/1548194/how-to-get-the-htmls-input-element-of-file-type-to-only-accept-pdf-files/11231236 see here – Dorvalla Jan 28 '22 at 19:59
  • Fixed it! it was the $check["mime"] that was giving me an error because the file wasn't an image. I also changed the "application/pdf" extension like you said and now it works, the file has been uploaded :) thank you so much for your help. – josefdzbeta Jan 28 '22 at 20:10

1 Answers1

0
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["archivoSubir"]["name"]);
$uploadOk = 1;
$tipoFichero = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(isset($_POST["submit"])) {
  $uploadOk = 1;
}else {
  $uploadOk = 0;
 }
   
// Limitar formato de archivos
 if($tipoFichero != "jpg" && $tipoFichero != "png" && $tipoFichero != "jpeg"
&& $tipoFichero != "gif" && $tipoFichero != "application/pdf" && $tipoFichero != "pdf") {
echo 'Lo siento, solo archivos JPG, JPEG, PNG, GIF & PDF están permitidos.';
print_r($_FILES);
$uploadOk = 0;
} 

// Limitar tamaño de fichero
 if ($_FILES["archivoSubir"]["size"] > 5000000) {
  echo 'El archivo que intentas subir es demasiado grande.';
  $uploadOk = 0;
 }
// Comprobamos si el archivo ya existe
if (file_exists($target_file)) {
  echo 'El archivo ya existe.';
  $uploadOk = 0;
 }

// Comprobar si ha habido un error en la subida
 if ($uploadOk == 0) {
  echo 'El archivo no se ha podido subir.';
  // Si todo está bien, subimos el archivo
  } else {
    if (move_uploaded_file($_FILES["archivoSubir"]["tmp_name"], $target_file)) {
     echo 'El archivo '. htmlspecialchars( basename( $_FILES["archivoSubir"]["name"])). ' ha sido subido.';
     header( "refresh:2;url=test.php" );
   } else {
    echo 'Ha habido un error al subir el archivo.';
   }
 }


your html code should be..

   <form action="test.php" method="post" enctype="multipart/form-data">
   Select image to upload:
    <input type="file" name="archivoSubir" id="archivoSubir">
    <input type="submit" value="Upload Image" name="submit">
   </form> 

your need to remove image validation as your uploading pdf as well i.e

 remove $check = getimagesize($_FILES["archivoSubir"]["tmp_name"]);

i.e
   // Comprobar si el archivo actual es una imagen o no
    if(isset($_POST["submit"])) {      
      $uploadOk = 1;        
    } else {
      $uploadOk = 0;
     }

you need to check/add $tipoFichero != "pdf" i.e

   // Limitar formato de archivos
     if($tipoFichero != "jpg" && $tipoFichero != "png" && $tipoFichero != "jpeg" && $tipoFichero != "gif" && $tipoFichero != "application/pdf" && $tipoFichero != "pdf") 
      {
       echo 'Lo siento, solo archivos JPG, JPEG, PNG, GIF & PDF están permitidos.';
       print_r($_FILES);
       $uploadOk = 0;
      }
Suhasini
  • 91
  • 1
  • 4