944,205 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 539
  • PHP RSS
Oct 5th, 2009
-1

PHP upload issues - uploading not working on some machines

Expand Post »
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.

php Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 5th, 2009
2

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

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.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 5th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by Atli ...
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.
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 5th, 2009
0

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

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)?
Reputation Points: 18
Solved Threads: 2
Posting Whiz
Venom Rush is offline Offline
325 posts
since Oct 2007
Oct 5th, 2009
0

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

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:
php Syntax (Toggle Plain Text)
  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".
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 5th, 2009
0

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

Sometimes I make the common mistake of not setting enctype in the HTML form?
Reputation Points: 13
Solved Threads: 13
Junior Poster
liamfriel is offline Offline
101 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: two databases issue
Next Thread in PHP Forum Timeline: HELP Cron job PHP error filling log file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC