Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP] Klasa upload
potreb
post
Post #1





Grupa: Zarejestrowani
Postów: 1 568
Pomógł: 192
Dołączył: 7.03.2005
Skąd: Warszawa

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


Witam. Napisałem bardzo prostą klasę uploadu plików na serwer. Proszę o jakieś uwagi.
Całość u mnie działa, więc teraz potrzebuję uwag, co można zmienić, wyrzucić itp.

  1. class Upload {
  2. public $extensions = array('.jpg','.png','.gif','.doc','.rtf','.pdf','.sql','.txt');
  3. public $prefix;
  4. public $max_size;
  5. public $cf;
  6. public $info;
  7.  
  8. private $tmp_name;
  9. private $name;
  10. private $error;
  11. private $size;
  12.  
  13.  
  14. public function __construct() {
  15. if(isset($_FILES['file']) && is_array($_FILES['file'])) {
  16. $this->tmp_name = $_FILES['file']['tmp_name'];
  17. $this->name = $_FILES['file']['name'];
  18. $this->error = $_FILES['file']['error'];
  19. $this->size = $_FILES['file']['size'];
  20. }
  21.  
  22. $this->max_size = 200000;
  23. $this->cf = 3;
  24. }
  25.  
  26. private function uploadExt($v) {
  27. $str = (strpos($v,'.')===false?'':'.'.substr(strrchr($v, "."), 1));
  28. return $str;
  29. }
  30.  
  31. private function uploadName($v) {
  32. $str = preg_replace('/[^0-9a-zA-Z-\_.]/', '', $v);
  33. return $str;
  34. }
  35.  
  36. private function uploadPrefix()
  37. {
  38. if(!$this->prefix) {
  39. $prefix = date("ymds_");
  40. return $prefix;
  41. } else {
  42. return $this->prefix;
  43. }
  44. }
  45.  
  46. public function uploadFiles($upload_dir)
  47. {
  48. for($i=0; $i<$this->cf;$i++)
  49. {
  50. if($this->name[$i])
  51. {
  52. if(in_array($this->uploadExt($this->name[$i]), $this->extensions))
  53. {
  54. if($this->size[$i] <= $this->max_size)
  55. {
  56. $target = $upload_dir . $this->uploadPrefix() . $this->uploadName($this->name[$i]);
  57. if(move_uploaded_file($this->tmp_name[$i],$target)) {
  58. $this->info[] = "Plik został wysłany";
  59. } else {
  60. $this->info[] = "Nie można wysłać pliku";
  61. }
  62.  
  63. } else
  64. {
  65. $this->info[] = "Rozmiar pliku przekracza dopuszczalną wartość!";
  66. }
  67. } else
  68. {
  69. $this->info[] = "Plik o tym rozszerzeniu nie może być wysłany!";
  70. }
  71. } else {
  72. $this->info[] = "Plik nie został wybrany";
  73. }
  74. }
  75. }
  76.  
  77. }
  78.  
  79. $upload = new Upload;
  80. $upload->cf = 2;
  81.  
  82. if(isset($_POST['upload'])) {
  83. $upload->max_size = 144550333;
  84. $upload->extensions = array('.png','.sql','.txt');
  85.  
  86. $upload->uploadFiles('./files/');
  87. foreach($upload->info as $value) {
  88. echo "<p>".$value."</p>";
  89. }
  90. }
  91.  
  92.  
  93.  
  94. echo "<form action='index.php' method='post' enctype='multipart/form-data'>";
  95. for($i=0;$i<$upload->cf;$i++) {
  96. echo "<p><input name='file[]' type='file' /></p>";
  97. }
  98. echo "<p><input type='submit' name='upload' value='Upload' /></p></form>";


--------------------

Go to the top of the page
+Quote Post

Posty w temacie


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 Aktualny czas: 19.08.2025 - 15:20