pastilhex Posted September 7, 2011 Share Posted September 7, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/ Share on other sites More sharing options...
pein87 Posted September 8, 2011 Share Posted September 8, 2011 Since the extension is given in the files name you can just substr that from the file name. Or you could regex it to return everything after the . but you'd need to check its distance for the end of the string to be sure its accurate. Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266676 Share on other sites More sharing options...
PFMaBiSmAd Posted September 8, 2011 Share Posted September 8, 2011 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...) Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266679 Share on other sites More sharing options...
pastilhex Posted September 8, 2011 Author Share Posted September 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266798 Share on other sites More sharing options...
PFMaBiSmAd Posted September 8, 2011 Share Posted September 8, 2011 $_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. Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266820 Share on other sites More sharing options...
pastilhex Posted September 8, 2011 Author Share Posted September 8, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266846 Share on other sites More sharing options...
PFMaBiSmAd Posted September 8, 2011 Share Posted September 8, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266850 Share on other sites More sharing options...
pastilhex Posted September 8, 2011 Author Share Posted September 8, 2011 Thank you very much for the quick help! Quote Link to comment https://forums.phpfreaks.com/topic/246661-mime-type-not-always-recognizing-pdf/#findComment-1266860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.