Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Skrypt dodający obrazki nie działa
olo707
post 20.04.2012, 12:39:50
Post #1





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 30.03.2012

Ostrzeżenie: (0%)
-----


Witam!
Mam strone na serwerach cba i friko o adresach http://chenoveth.cba.pl i http://www.chenoveth.za.pl Umieściłem w niej skrypt umożliwiający dodawanie obrazków (tylko w formacie jpg/jpeg i gif). Działanie skryptu polega na tym że w momencie dodania pierwszego obrazku (pierwszego obrazku do skryptu w ogóle) zostanie wygenerowany folder o nazwie uploads. Następnie w nim zostanie wygenerowany kolejny folder o unikalnej nazwie nadanej przez skrypt (ze znaków 1-9 i a-z np:jk6k8a12). W tym właśnie folderze zostanie umieszczony obrazek dodany przez użytkownika również o unikalnej nazwie nadanej przez skrypt (ze znaków 1-9 i a-z np:g8vlm.jpg) i wygenerowana będzie kopia tego obrazku (również w tym folderze co uploadowany obrazek; jk6k8a12 ) w innych rozmiarach np: 250x250px o nazwie w tym przypadku: thumb_g8vlm.jpg. W przypadku kolejnego dodania obrazku proces się powtarza. W utworzonym już folderze głównym uploads zostanie wygenerowany kolejny folder o unikalnej nazwie gdzie umieszczony bedzie dodany przez użytkownika obrazek (i dodana do niego unikalna nazwa) i jego wygenerowana kopia. Na komputerze domowym localhoscie działa to idealnie ale na serwerach cba i friko nie. Na serwerze cba nie może utworzyć kopii obrazku. Tworzy folder główny uploads tworzy foldery w nim zawarte i umieszcza w nich dodawane obrazki ale nie może utworzyć kopii tych obrazków. Natomiast na serwerze friko nawet nie tworzy głównego katalogo uploads. Nie wiem czy to jest wina skryptu czy może ustawień tych serwerów.
Skrypt ten wygląda tak:
  1. <?php
  2. class CustomException extends Exception{}
  3.  
  4. class add_model extends Model
  5. {
  6. private $_errors;
  7. private $_token; //Form token
  8.  
  9. private $_rootDir; //Main folder for our uploads
  10.  
  11. private $_imgName; //Image name randomly generated
  12. private $_imgDest; //Direct file location on server
  13.  
  14. private $_folderName; //Folder name randomly generated
  15. private $_folderPath; //Include root dir
  16.  
  17. private $_thumbWidth;
  18. private $_thumbHeight;
  19. private $_thumbPrefix;
  20. private $_thumbDest;
  21.  
  22. private $_fileName;
  23. private $_fileTemp;
  24. private $_fileSize;
  25. private $_fileType;
  26. private $_fileExt;
  27. private $_desc;
  28.  
  29.  
  30.  
  31. public function __construct($var = array())
  32. {
  33. $this->_errors = array();
  34. $this->_token = $_POST['token'];
  35. $this->_desc = $this->filter(($_POST['desc']));
  36.  
  37.  
  38. $this->_fileName = $_FILES['file']['name'];
  39. $this->_fileTemp = $_FILES['file']['tmp_name'];
  40. $this->_fileSize = $_FILES['file']['size'] / 1024;
  41. $this->_fileType = $_FILES['file']['type'];
  42. $this->_fileExt = ereg(".([^\.]+)$",$this->_fileName,$r)? strtolower($r[1]) : '';
  43.  
  44. $this->_rootDir = empty($var['RootDir'])? 'uploads/' : $var['RootDir'];
  45.  
  46. $this->_folderName = $this->randString(8);
  47. $this->_folderPath = $this->_rootDir.$this->_folderName.'/';
  48.  
  49. $this->_imgName = $this->randString(6).'.'.$this->_fileExt;
  50. $this->_imgDest = $this->_folderPath.$this->_imgName; // uploads/76sd7/798234.jpg
  51.  
  52. $this->_thumbWidth = empty($var['ThumbWidth'])? 250 : $var['ThumbWidth'];
  53. $this->_thumbHeight = empty($var['ThumbHeight'])? 250 : $var['ThumbHeight'];
  54. $this->_thumbPrefix = empty($var['ThumbPrefix'])? 'thumb_' : $var['ThumbPrefix'];
  55. $this->_thumbDest = $this->_folderPath.$this->_thumbPrefix.$this->_imgName;
  56. // uploads/76sd7/thumb_798234.jpg
  57. }
  58. public function filter($var)
  59. {
  60.  
  61. return preg_replace('/[^a-ząęółśżźćńA-ZŁŚ0-9_.,:;\s\$]/','',$var);
  62.  
  63.  
  64. }
  65. public function isDescValid()
  66. {
  67. return (!empty($this->_desc) && !eregi('^[a-ząęółśżźćńA-ZŁŚ0-9_.,:;\s \$]+$',$this->_desc)) ? 0 : 1;
  68. }
  69. public function lenghDesc()
  70. {
  71. return (strlen($this->_desc)> 100) ? 0 : 1;
  72. }
  73.  
  74. public function process()
  75. {
  76. try
  77. {
  78.  
  79. if(!$this->isTokenValid())
  80. throw new Exception('Invalid form submission');
  81. if(!$this->isImageValid())
  82. throw new Exception('Invalid image type and/or size');
  83. if(!$this->lenghDesc())
  84. throw new Exception('Max limit for description are 100 characters');
  85. if(!$this->isDescValid())
  86. throw new Exception('Your description contains invalid characters');
  87. if(!$this->saveImage())
  88. throw new Exception('Could not save image');
  89.  
  90. return true;
  91. }
  92. catch(Exception $e)
  93. {
  94. $this->_errors[] = $e->getMessage();
  95. return false;
  96. }
  97. }
  98.  
  99. public function saveImage()
  100. {
  101. try
  102. {
  103. if(!$this->createImageDir())
  104. throw new Exception();
  105. if(!$this->moveUploadedFile())
  106. throw new CustomException();
  107. if(!$this->createImageThumb())
  108. throw new CustomException();
  109. if(!$this->saveToDatabase())
  110. throw new CustomException();
  111.  
  112. return true;
  113. }
  114. catch(Exception $e)
  115. {}
  116. catch(CustomException $e)
  117. {
  118. $this->removeImageDir();
  119. }
  120. return false;
  121. }
  122.  
  123. public function createImageDir()
  124. {
  125. if(!is_dir($this->_rootDir))
  126. mkdir($this->_rootDir,0777);
  127.  
  128. if(!is_dir($this->_folderPath))
  129. {
  130. mkdir($this->_folderPath,0777);
  131. return true;
  132. }
  133. else
  134. return false;
  135. }
  136.  
  137. public function removeImageDir()
  138. {
  139. rmdir($this->_folderPath);
  140.  
  141. return is_dir($this->_folderPath)? 0 : 1;
  142. }
  143.  
  144. public function moveUploadedFile()
  145. {
  146. @move_uploaded_file($this->_fileTemp,$this->_imgDest);
  147. @chmod($this->_imgDest,octdec(0755));
  148.  
  149. return file_exists($this->_imgDest)? 1 : 0;
  150. }
  151.  
  152. public function isImageValid()
  153. {
  154. return (($this->_fileExt != "jpg" && $this->_fileExt != "jpeg" && $this->_fileExt != "gif") || ($this->_fileSize > 2048))? 0 : 1;
  155. }
  156.  
  157. public function isTokenValid()
  158. {
  159. return (!isset($_SESSION['token']) || $this->_token != $_SESSION['token'])? 0 : 1;
  160. }
  161.  
  162. public function saveToDatabase()
  163. {
  164. mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
  165.  
  166. mysql_query("INSERT INTO store (filename,folder,para) VALUES ('{$this->_imgName}','{$this->_folderName}','{$this->_desc}')");
  167.  
  168. return mysql_affected_rows()? 1 : 0;
  169. }
  170.  
  171. public function createImageThumb()
  172. {
  173. if($this->_fileExt == "jpg")
  174. {
  175. //create new image from file
  176.  
  177.  
  178. $img = imagecreatefromjpeg($this->_imgDest);
  179.  
  180.  
  181. //get image size
  182. $width = imagesx($img);
  183. $height = imagesy($img);
  184.  
  185. // calculate thumbnail size
  186. $new_width = $this->_thumbWidth;
  187. $new_height = floor($height * ($this->_thumbWidth/$width));
  188.  
  189. // create a new temporary image
  190. $tmp_img = imagecreatetruecolor($new_width,$new_height);
  191.  
  192. // copy and resize old image into new image
  193. imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height,$width,$height);
  194.  
  195. // save thumbnail into a file
  196. imagejpeg($tmp_img, $this->_thumbDest) ;
  197.  
  198.  
  199. return file_exists($this->_thumbDest)? 1 : 0;
  200. }
  201. else if($this->_fileExt == "gif")
  202. {
  203. $img = imagecreatefromgif($this->_imgDest);
  204.  
  205. //get image size
  206. $width = imagesx($img);
  207. $height = imagesy($img);
  208.  
  209. // calculate thumbnail size
  210. $new_width = $this->_thumbWidth;
  211. $new_height = floor($height * ($this->_thumbWidth/$width));
  212.  
  213. // create a new temporary image
  214. $tmp_img = imagecreatetruecolor($new_width,$new_height);
  215.  
  216. // copy and resize old image into new image
  217. imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height,$width,$height);
  218.  
  219. // save thumbnail into a file
  220. imagegif($tmp_img, $this->_thumbDest);
  221.  
  222. return file_exists($this->_thumbDest)? 1 : 0;
  223. }
  224. }
  225.  
  226. public function showErrors()
  227. {
  228.  
  229.  
  230. foreach($this->_errors as $key=>$value)
  231. echo "<span id='errorsadd'>$value</span>";
  232. }
  233.  
  234. public function randString($length = 5)
  235. {
  236. $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
  237.  
  238. for($x=0; $x<$length; $x++)
  239. $string.= $characters[mt_rand(0,strlen($characters))];
  240.  
  241. return $string;
  242. }
  243.  
  244.  
  245. }
  246.  
  247. ?>
  248.  

Dodawanie obrazków wymaga rejestracji i zalogowania co zajmuje góra 2 min (nie ma żadnych zwrotnych emaili z linkami aktywacyjnymi)

Będe wdzięczny za pomoc
Pozdro

Ten post edytował olo707 20.04.2012, 19:22:18
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
Spawnm
post 20.04.2012, 12:45:51
Post #2





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Proszę dodać bbcode.
Go to the top of the page
+Quote Post
olo707
post 20.04.2012, 12:51:16
Post #3





Grupa: Zarejestrowani
Postów: 77
Pomógł: 0
Dołączył: 30.03.2012

Ostrzeżenie: (0%)
-----


jakiego bbcoda sprecyzuj o co Ci chodzi
Go to the top of the page
+Quote Post
maniana
post 20.04.2012, 14:59:56
Post #4





Grupa: Zarejestrowani
Postów: 207
Pomógł: 44
Dołączył: 18.05.2007

Ostrzeżenie: (0%)
-----


Przy dodawaniu/edycji posta klikasz w button PHP i zamykasz w znacznikach kod. Będzie on wtedy sformatowany.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 5.07.2025 - 19:55