Jump to content

mime type not always recognizing pdf


pastilhex

Recommended Posts

Hi there everyone!

I'm working on uploader script that checks the extension files to comprove that the file is always an pdf extension.

I've tried to upload diferent files, all of them pdf's but strainglly some pdf's are accepted and others not.

Can someone please take a look to my code?

<?php
if (($_FILES["file"]["type"] == "application/pdf") // After checking the extension here some pdf files are accepted and others are not
&& ($_FILES["file"]["size"] < 1000000))
  {
  if ($_FILES["file"]["error"] > 0)
{
echo 'Problema: ';
	switch ($_FILES['userfile']['error'])
	{
	case 1: echo 'File exceeded upload_max_filesize';
	break;
	case 2: echo 'File exceeded max_file_size';
	break;
	case 3: echo 'File only partially uploaded';
	break;
	case 4: echo 'No file uploaded';
	break;
	case 6: echo 'Cannot upload file: No temp directory specified';
	break;
	case 7: echo 'Upload failed: Cannot write to disk';
	break;
	}
	exit;
}
  else
{
echo "Enviado: " . $_FILES["file"]["name"] . "<br />";
echo "Tipo: " . $_FILES["file"]["type"] . "<br />";
echo "Tamanho: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Ficheiro Temporário: " . $_FILES["file"]["tmp_name"] . "<br />";
   /*  if (file_exists("horario/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " já existe. ";
  }
else
  { */
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "horario/" . $_FILES["file"]["name"]);
  echo "Guardado em: " . "horario/" . $_FILES["file"]["name"];
  rename ("horario/".$_FILES["file"]["name"], "horario/horario.pdf");
  echo "<br><input type='button' value='Voltar' onClick='history.go(-1)'>";
  /* } */
}
  }
else
  {
  echo "Ficheiro inválido"; // If the pdf has been refused i get this error msg - it means invalid file
  }
?>

Link to comment
Share on other sites

Your code (which is probably based on the w3schools example) does not check for uploaded errors first. You cannot reference the ["type"] or ["size"] information until after you have tested that the upload worked.

 

You should also not group the  ["type"] and ["size"] test together in one statement, because you won't know which one of those values caused the test to fail. You should have separate tests with unique error messages that tell the visitor what was wrong with the uploaded file (the type was not correct..., the size was too large...)

 

 

Link to comment
Share on other sites

I reframed the code and the first thing that the code check's its the $_FILES["file"]["error"] > 0

<?php
if ($_FILES["file"]["error"] > 0){
	echo 'Problema: ';
		switch ($_FILES['userfile']['error'])
		{
			case 1: echo 'File exceeded upload_max_filesize';
			break;
			case 2: echo 'File exceeded max_file_size';
			break;
			case 3: echo 'File only partially uploaded';
			break;
			case 4: echo 'No file uploaded';
			break;
			case 6: echo 'Cannot upload file: No temp directory specified';
			break;
			case 7: echo 'Upload failed: Cannot write to disk';
			break;
		}
		exit;
}else{
?>

I'm not really understanding where is the mistake. I have some files to upload.

Some of them are uploaded correctly and others don't (i don't get any error msg) but they all are pdf files.

Even with the error checking over the code i can't upload the file and worst of all i'm not receiving any error message.

Link to comment
Share on other sites

$_FILES['userfile'] should be $_FILES["file"]

 

You should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. You will save a TON of time. The above variable name mismatch would have resulted in an undefined variable error/message, assuming that branch of the code is being executed, you didn't actually state if you are getting the 'Problema: ' output.

Link to comment
Share on other sites

Hello PFMaBiSmAd,

You are absolutely right, i was missing that wrong name.

Has you easily noted i'm giving my first step's in php. Has you said it would be interesting to work with a code debugger that easily highlight some code errors or mismatchs.

Could you kindly recommend me one of the systems that mentioned with error_reporting?

Thanks :)

 

PS: Sorry about my english...

Link to comment
Share on other sites

error_reporting and display_errors are two php settings.

 

You should set them in your master php.ini so that fatal parse errors in your main files will be reported, but you can also set them in your code (won't show fatal parse errors in your main file). To set them in your code, use the following -

 

ini_set("display_errors", "1");
error_reporting(-1);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.