Advertisement
Guest User

upload

a guest
May 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. <?php
  2.  
  3. //Errors
  4. ini_set('display_errors', 1);
  5. ini_set('display_startup_errors', 1);
  6. error_reporting(E_ALL);
  7. //Errors
  8.  
  9. // Session
  10. if (!session_id()) {
  11. session_start();
  12. }
  13. $ses_id = session_id();
  14. // Session
  15.  
  16. require '../classes/db.php';
  17.  
  18. class imgUploader
  19. {
  20. public $exts = array( ".png", ".gif", ".png", ".jpg", ".jpeg" ); //all the extensions that will be allowed to be uploaded
  21. public $maxSize = 1024*1024*10; //if you set to "0" (no quotes), there will be no limit
  22. public $uploadTarget = "../uploads/"; //make sure you have the '/' at the end
  23. public $fileName = ""; //this will be automatically set. you do not need to worry about this
  24. public $tmpName = ""; //this will be automatically set. you do not need to worry about this
  25.  
  26.  
  27. public function startUpload($file, $logoType, $id, $ses_id)
  28. {
  29. $this->fileName = $file['name'][$logoType];
  30. $this->tmpName = $file['tmp_name'][$logoType];
  31. $this->tempExt = explode('/',$file['type'][$logoType]);
  32. $this->extension = $this->tempExt[1];
  33. $this->id = $id;
  34. $this->ses_id = $ses_id;
  35. $this->logoType = $logoType;
  36.  
  37.  
  38. if( !$this->isWritable() )
  39. {
  40. die( '{"status":0,"data":null,"error":["Musisz ustawić CHMOD na 777"]}' );
  41. }
  42. if( !$this->checkExt() )
  43. {
  44. die( '{"status":0,"data":null,"error":["Plik obrazka jest nieprawidłowy"]}' );
  45. }
  46. if( !$this->checkSize() )
  47. {
  48. die( '{"status":0,"data":null,"error":["Plik jest zbyt duży"]}' );
  49. }
  50. if( $this->uploadIt() )
  51. {
  52. //echo '{"status":1,"data":null,"error":[]}';
  53. }
  54. if( $fileNameToSave = $this->getFileName() )
  55. {
  56. echo '{"status":1,"data":null,"error":[]}';
  57. }
  58. else
  59. {
  60. echo '{"status":0,"data":null,"error":["Wystąpił błąd"]}';
  61. }
  62. }
  63.  
  64. // Blob
  65.  
  66. public function startUpload2($file, $logoType, $id, $ses_id)
  67. {
  68. $this->fileName = $file['name'];
  69. $this->tmpName = $file['tmp_name'];
  70. $this->id = $id;
  71. $this->ses_id = $ses_id;
  72. $this->logoType = $logoType;
  73.  
  74. if( !$this->isWritable() )
  75. {
  76. die( '{"status":0,"data":null,"error":["Musisz ustawić CHMOD na 777"]}' );
  77. }
  78. if( !$this->checkSize() )
  79. {
  80. die( '{"status":0,"data":null,"error":["Plik jest zbyt duży"]}' );
  81. }
  82. if( $this->uploadIt2() )
  83. {
  84. //echo '{"status":1,"data":null,"error":[]}';
  85. }
  86.  
  87. if( $fileNameToSave = $this->getFileName2() )
  88. {
  89. //echo '{"status":1,"data":null,"error":[]}';
  90. }
  91. if( $this->saveDataToDB($fileNameToSave, $logoType, $id, $ses_id) )
  92. {
  93. echo '{"status":1,"data":null,"error":[]}';
  94. }
  95.  
  96. else
  97. {
  98. echo '{"status":0,"data":null,"error":["Wystąpił błąd"]}';
  99. }
  100. }
  101.  
  102. public function uploadIt()
  103. {
  104. return ( move_uploaded_file( $this->tmpName, $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType . '.' . $this->extension ) ? true : false );
  105. }
  106. public function uploadIt2()
  107. {
  108. return ( move_uploaded_file( $this->tmpName, $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType ) ? true : false );
  109. }
  110.  
  111. public function checkSize()
  112. {
  113. return ( ( filesize( $this->tmpName ) > $this->maxSize ) ? false : true );
  114. }
  115.  
  116. public function getExt()
  117. {
  118. return strtolower( substr( $this->fileName, strpos( $this->fileName, "." ), strlen( $this->fileName ) - 1 ) );
  119. }
  120.  
  121. public function checkExt()
  122. {
  123. return ( in_array( $this->getExt(), $this->exts ) ? true : false );
  124. }
  125.  
  126. public function isWritable()
  127. {
  128. return ( is_writable( $this->uploadTarget ) );
  129. }
  130. public function getFileName2()
  131. {
  132. return $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType;
  133. }
  134. public function getFileName()
  135. {
  136. return $this->uploadTarget . $this->id . '-' . $this->ses_id . '-' . $this->logoType . '.' . $this->extension;
  137. }
  138. public function saveDataToDB($fileNameToSave, $logoType, $id, $ses_id)
  139. {
  140. $stmt = $pdo->prepare("UPDATE a_temp_session
  141. SET {$logoType} = :value
  142. WHERE session_id = :session_id AND id=:id");
  143.  
  144. $stmt->bindValue(":session_id", $ses_id);
  145. $stmt->bindValue(":id", $id);
  146. $stmt->bindValue(":value", $fileNameToSave);
  147. $stmt->execute();
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement