PHP upload issues - uploading not working on some machines

Thread Solved

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

PHP upload issues - uploading not working on some machines

 
-1
  #1
Oct 5th, 2009
Hi all

I have some functionality that allows the upload of files such as MS word docs, zips etc. It works fine on my pc and another test machine but doesn't seem to work on two other machines I've tested. All machines have Windows XP installed. The first machine that is having problems only has issues uploading PDF's. The second machine having problems can't upload anything at all.

Please could someone have a look at my code below and tell me if there's anything there that could be causing these problems. I'm pretty sure I have the MIME types right. I'm really puzzled as to what is causing this.

P.S. My code isn't the best in the world. Note that this upload script is not being used publicly on the website so security issues aren't top priority.

  1. class DocumentUpload
  2. {
  3. //Variables values gained from the __construct() method
  4. public $inputName;
  5. public $original;
  6. public $timeOriginal;
  7. public $tempName;
  8. public $docType;
  9. public $folder;
  10. public $dbFolder;
  11. public $page;
  12. public $pathAndFile;
  13. public $pathTimeFile;
  14. public $dbFile;
  15. public $dbTimeFile;
  16. //Variables used in the upload() method
  17. public $thumb;
  18. public $success;
  19. public $result;
  20. public $mailSubject = "";
  21. //Variable used to see which path and filename must be added to the database
  22. public $timedFile;
  23.  
  24. //Array of acceptable MIME types
  25. public $permitted = array('application/pdf', 'application/zip', 'application/msword', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel');
  26. //variables set assuming the file is unacceptable
  27. public $sizeOK = false;
  28. public $typeOK = false;
  29.  
  30. public function __construct($inputName, $folder, $dbFolder, $page)
  31. {
  32. $this->inputName = $inputName;
  33. $this->original = str_replace(array(" ", 'PDF'), array("_", 'pdf'), $_FILES[''.$inputName.'']['name']);
  34. $this->timeOriginal = time().$this->original;
  35. $this->tempName = $_FILES[''.$inputName.'']['tmp_name'];
  36. $this->docType = $_FILES[''.$inputName.'']['type'];
  37. $this->folder = $folder;
  38. $this->dbFolder = $dbFolder;
  39. $this->pathAndFile = $folder.$this->original;
  40. $this->pathTimeFile = $folder.time().$this->original;
  41. $this->dbFile = $dbFolder.$this->original;
  42. $this->dbTimeFile = $dbFolder.time().$this->original;
  43. $this->page = $page;
  44. }
  45.  
  46.  
  47. //Upload an image to the server
  48. public function upload($chmod)
  49. {
  50. //check that the file is of a permitted MIME type
  51. foreach($this->permitted as $type)
  52. {
  53. if($type == $this->docType)
  54. {
  55. $this->typeOK = true;
  56. break;
  57. }
  58. }
  59.  
  60. //Check if the file type has been verified as valid
  61. if($this->typeOK)
  62. {
  63. switch($_FILES[''.$this->inputName.'']['error'])
  64. {
  65. case 0:
  66. // Make sure file of the same name does not exist
  67. if (!file_exists($this->pathAndFile))
  68. {
  69. $this->success = move_uploaded_file($this->tempName, $this->pathAndFile);
  70. chmod($this->pathAndFile, $chmod);
  71.  
  72. //Variable used to see which path and filename must be added to the database
  73. $this->timedFile = false;
  74. }
  75. else
  76. {
  77. $this->success = move_uploaded_file($this->tempName, $this->pathTimeFile);
  78. chmod($this->pathTimeFile, $chmod);
  79.  
  80. //Variable used to see which path and filename must be added to the database
  81. $this->timedFile = true;
  82. }
  83.  
  84. //If the file was uploaded
  85. if ($this->success)
  86. {
  87. $this->result = "Document added successfully.";
  88. }
  89. else
  90. {
  91. $this->result = "There was an error uploading the file. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>.";
  92. }
  93. break;
  94. case 3:
  95. $this->result = "There was an error uploading the file. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>.";
  96. default:
  97. $this->result = "System error uploading file. Contact the <a class=\"clrBrown\" href=\"mailto:test@website.com".$this->mailSubject."\">webmaster</a>";
  98. }
  99. }
  100. else if($_FILES[''.$this->inputName.'']['error'] == 4)
  101. {
  102. $this->result = 'No file selected';
  103. }
  104. else
  105. {
  106. $this->result = "File cannot be uploaded. Acceptable file types: ppt, xls, doc, pdf, zip. <a class=\"clrBrown\" href='".$this->page."' >Please try again</a>.";
  107. }
  108. }
  109. }
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: PHP upload issues - uploading not working on some machines

 
2
  #2
Oct 5th, 2009
Hey.

Looking over that, I see nothing essentially wrong with it.
I assume you have ruled out the usual file permissions issues and upload size limitations?

When the code is not working, does it give you any sort of error messages?
(Are error messages even turned on in the server?)

One the server that only blocked PDF files, did you try printing the mime type that you were actually getting?
Also, keep in mind that the type that is sent with the file is sent by the browser, so it is not 100% reliable or consistent.
(Different browsers may use different mime types for the same file type)

P.S.
$array[''.$name.''] should be $array[$name] .
The surrounding strings in all your array element names are not needed. Serve no purpose.
Last edited by Atli; Oct 5th, 2009 at 8:09 am.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: PHP upload issues - uploading not working on some machines

 
0
  #3
Oct 5th, 2009
Originally Posted by Atli View Post
Hey.

Looking over that, I see nothing essentially wrong with it.
I assume you have ruled out the usual file permissions issues and upload size limitations?

When the code is not working, does it give you any sort of error messages?
(Are error messages even turned on in the server?)

One the server that only blocked PDF files, did you try printing the mime type that you were actually getting?
Also, keep in mind that the type that is sent with the file is sent by the browser, so it is not 100% reliable or consistent.
(Different browsers may use different mime types for the same file type)

P.S.
$array[''.$name.''] should be $array[$name] .
The surrounding strings in all your array element names are not needed. Serve no purpose.
Yes, file permission and upload size are not the issue. The server hasn't got error messaging turned on. I'll see if I can get that sorted out. I haven't echo'd the mime type to see if there's anything difference on the two problematic machines from the two working machines, but I'll do so and see if that's the issue (didn't know that browsers gave different mime types).

Thanks for your tip in your P.S.
Last edited by Venom Rush; Oct 5th, 2009 at 8:15 am.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: PHP upload issues - uploading not working on some machines

 
0
  #4
Oct 5th, 2009
Ok, so it seems it was the MIME types in the end.

For future reference, is there any other way to find out the MIME type of a file (other than checking the file extension based on the name)?
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 438
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: PHP upload issues - uploading not working on some machines

 
0
  #5
Oct 5th, 2009
You can try the FileInfo module. It looks for certain byte-sequences within files to guess their mime type.

For images, you can use the getimagesize function. It returns the actual type of the image based on the data, rather then any outside info.

For PDF files, if you do not use FileInfo, you can read the first few bits of the file and check if they are really valid headers for a PDF document. All valid PDF documents should start with something like %PDF , so you could do:
  1. <?php
  2. function validate_pdf($path) {
  3. $fh = fopen($path, "rb");
  4. if($fh) {
  5. $header = fread($fh, 4);
  6. if($header == "%PDF") {
  7. return true;
  8. }
  9. else {
  10. return false;
  11. }
  12. }
  13. else {
  14. return false;
  15. }
  16. }
  17. ?>
It's not exactly bullet-proof, but it should at least filter out files that are just renamed to ".pdf".
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 26
Reputation: liamfriel is an unknown quantity at this point 
Solved Threads: 1
liamfriel's Avatar
liamfriel liamfriel is offline Offline
Light Poster

Re: PHP upload issues - uploading not working on some machines

 
0
  #6
Oct 5th, 2009
Sometimes I make the common mistake of not setting enctype in the HTML form?
They throw us away like yesterdays jam - Maurice Mossley

Please - rep if someone helps you, it can't be traded for stuff, but it's nice.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC