Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][HTML] multi Upload plików
Forum PHP.pl > Forum > Przedszkole
XP'ek
Witam,

mam taki upload php do upladowaniu kilku plików na raz i chodzi o sprawdzenie co z nim jest nie tak to raz a dwa zrobić przechwytywanie nazw do bazy danych

plik upload.class.php
  1. <?
  2. ///////////////////////////////////////
  3. //multi-files upload class
  4. //author:paul.ren
  5. //e-mail:rsr_cn@yahoo.com.cn
  6. //website:www.yawill.com
  7. //create:2004-6-22 09:56
  8. //modify:
  9. ////////////////////////////////////////
  10.  
  11. class ClsUpload{
  12. var $max_upload_size = "1048576"; //max upload file byte (1M)
  13. var $_FILES = ""; //post files
  14. var $overwrite = "1"; //is over write
  15. var $mode = 0777; //upload file mode
  16. var $results = array(); //uploaded files results
  17.  
  18. var $error = "";
  19.  
  20. function ClsUpload(){
  21. global $_FILES;
  22. $this->_FILES = $_FILES;
  23. }
  24. function uploadfile($srcfile,$dstfile){
  25. if(is_file($dstfile)){
  26. if($this->overwrite){
  27. @unlink($dstfile) or $this->error .= "unable to overwrite $dstfile<br>";
  28. }else return 1;
  29. }
  30. @copy($srcfile,$dstfile) or $this->error .= "unable to upload $srcfile to $dstfile<br>";
  31. $isok = @chmod($dstfile,$this->mode) or $this->error .= is_file($dstfile)."change permissions for:$dstfile<br>";
  32. return $isok;
  33. }
  34.  
  35. function save($field,$dir,$file_name=""){
  36. $size = $this->_FILES[$field]['size'];
  37. if(is_array($size)) $size=0;
  38. if($size>0 && $size<=$this->max_upload_size){
  39. $this->error = $this->_FILES[$field]['error'];
  40. $tmp_file = $this->_FILES[$field]['tmp_name'];
  41. $file_type = $this->_FILES[$field]['type'];
  42. if(!$file_name) $file_name = $this->_FILES[$field]['name'];
  43. $save_path_file = $dir."/".strtolower($file_name);
  44. $isok = $this->uploadfile($tmp_file,$save_path_file);
  45. if($isok) $this->results[] = array($this->_FILES[$field]['name']=>$save_path_file);
  46. return $isok;
  47. }else if($size>$this->max_upload_size){
  48. $this->error .= "file size($size) exceeds max file size: $this->max_upload_size<br>";
  49. }else if($size==0) $this->error .= "File size is 0 bytes<br>";
  50. return 0;
  51. }
  52. function save_as($field,$pathfile){
  53. $dir = dirname($pathfile);
  54. $filename = basename($pathfile);
  55. return $this->save($field,$dir,$filename);
  56. }
  57. function mult_save($field,$dir){
  58. $field_names = $this->_FILES[$field]['name'];
  59. if(is_array($field_names)){
  60. while(list($key,$file_name)=each($field_names)){
  61. if(!$file_name) continue;
  62. $tmp_file = $this->_FILES[$field]['tmp_name'][$key];
  63. $file_name = strtolower($file_name);
  64. $file_size = $this->_FILES[$field]['size'][$key];
  65. if($file_size>0 && $file_size<=$this->max_upload_size){
  66. $isok = $this->uploadfile($tmp_file,"$dir/$file_name");
  67. }else if($file_size>$this->max_upload_size){
  68. $this->error .= "$file_name:file size exceeds max file size: $this->max_upload_size<br>";
  69. }else $this->error .= "$file_name:File size is 0 bytes<br>";
  70. if($isok) $this->results[] = array($file_name=>"$dir/$file_name");
  71. }
  72. }else {
  73. return $this->save($field,$dir);
  74. }
  75. return $isok;
  76. }
  77. function mult_save_as($field,$dir,$prefix="up_"){
  78. $field_names = $this->_FILES[$field]['name'];
  79. if(is_array($field_names)){
  80. while(list($key,$fname)=each($field_names)){
  81. if(!$fname) continue;
  82. $tmp_file = $this->_FILES[$field]['tmp_name'][$key];
  83. $file_name = strtolower("$prefix".$key.strrchr($fname,"."));
  84. $file_size = $this->_FILES[$field]['size'][$key];
  85. if($file_size>0 && $file_size <= $this->max_upload_size){
  86. $isok = $this->uploadfile($tmp_file,"$dir/$file_name");
  87. }else if($file_size>$this->max_upload_size){
  88. $this->error .= "$file_name:file size exceeds max file size: $this->max_upload_size<br>";
  89. }else $this->error .= "$file_name:File size is 0 bytes<br>";
  90. if($isok) $this->results[] = array($fname=>"$dir/$file_name");
  91. }
  92. }else {
  93. $filename = $prefix? ($prefix.strrchr(basename($this->_FILES[$field]['name']))):"";
  94. return $this->save($field,$dir,$filename);
  95. }
  96. return $isok;
  97. }
  98. function p($data){
  99. echo "<pre>";
  100. print_r($data);
  101. echo "</pre>";
  102. }
  103. }
  104.  
  105. ?>



oraz plik index.php

  1. <?
  2. //upload multi-files use mult_save_as method
  3. require "./upload.class.php";
  4. if($HTTP_POST_VARS[Submit]){
  5. $upload = new ClsUpload();
  6. $upload->mult_save_as("file",".","php_");
  7. $upload->p($upload);
  8. }
  9.  
  10.  
  11. ?>
  12. <html>
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  15. <title>upload file</title>
  16. </head>
  17. <body bgcolor="#E5E5E5" text="#000000" link="#006699" vlink="#5493B4">
  18. <form name="form1" method="post" enctype="multipart/form-data" >
  19. <input name="file[]" type="file" ><br/>
  20. <input name="file[]" type="file" ><br/>
  21. <input name="file[]" type="file" ><br/>
  22. <input type="submit" name="Submit" value="Submit">
  23. </form>
  24. </body>
  25. </html>
nospor
Cytat
i chodzi o sprawdzenie co z nim jest nie tak
A z tobą? (bez urazy, taka gra słów winksmiley.jpg )
Jak masz problem ze skryptem to napisz jaki masz problem a nie mamy się we wrózki bawić...

Cytat
z a dwa zrobić przechwytywanie nazw do bazy danych
A ty co już zrobiłeś ze swojej strony? Bo jak oczekujesz od nas gotowca to mogę temat przenieść na gieldę ofert.
XP'ek
nie oczekuje gotowca tylko podpowiedzi smile.gif a co z nim nie tak podczas uploadu wali cała clase wyświetla zobacz


Kod
ClsUpload Object
(
    [max_upload_size] => 1048576
    [_FILES] => Array
        (
            [file] => Array
                (
                    [name] => Array
                        (
                            [0] => wyszukaj.jpg
                            [1] => debian.JPG
                            [2] => bajak.jpg
                        )

                    [type] => Array
                        (
                            [0] => image/jpeg
                            [1] => image/jpeg
                            [2] => image/jpeg
                        )

                    [tmp_name] => Array
                        (
                            [0] => C:\Users\serwin\WebServ\temp\file-uploads\phpE24F.tmp
                            [1] => C:\Users\serwin\WebServ\temp\file-uploads\phpE250.tmp
                            [2] => C:\Users\serwin\WebServ\temp\file-uploads\phpE261.tmp
                        )

                    [error] => Array
                        (
                            [0] => 0
                            [1] => 0
                            [2] => 0
                        )

                    [size] => Array
                        (
                            [0] => 336364
                            [1] => 19217
                            [2] => 607243
                        )

                )

        )

    [overwrite] => 1
    [mode] => 511
    [results] => Array
        (
            [0] => Array
                (
                    [wyszukaj.jpg] => ./php_0.jpg
                )

            [1] => Array
                (
                    [debian.JPG] => ./php_1.jpg
                )

            [2] => Array
                (
                    [bajak.jpg] => ./php_2.jpg
                )

        )

    [error] =>
)


co prawda pliki sa na serwerze ale to wali a próbowałem z przechwyceniem to mam puste pole w db
nospor
No tak, bo wywolujesz funkcję P i jako jej parametr dajesz obiekt klasy:
$upload->p($upload);
A funkcja P robi wlasnie wyswietlanie tego co jej zapodasz smile.gif

Zas po obiekcie widać ze pliki przeslaly się poprawnie. Nic tylko się cieszyć smile.gif
XP'ek
czyli wywalić mam to

Kod
#
function p($data){
#
echo "<pre>";
#
print_r($data);
#
echo "</pre>";
#
}


ale czemu baza nie pobiera jak dodaje $filename
nospor
Cytat
ale czemu baza nie pobiera jak dodaje $filename
Pamiętasz jak mówiłem coś o wróżkach? tongue.gif
A skąd ja mam to wiedzieć? Nie pokazałeś kodu jak to robisz więc myślisz, że z Twojej głowy go magicznie wyciągnę?

Zanim napiszesz następnego posta zapoznaj się proszę z tematem:
Temat: Jak poprawnie zada pytanie
i zastosuj się do WSZYSTKICH porad
XP'ek
ok spox popróbuje jeszcze jak coś podam wszystko jak robię i zobaczymy
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.