multiple-file-upload-class

Gesponsorde koppelingen

PHP script bestanden

  1. multiple-file-upload-class

« Lees de omschrijving en reacties

++++++++++++++++++++++
Kopieer alles in een file met de naam

test_upload.htm
++++++++++++++++++++++

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" enctype="multipart/form-data" method="post" action="save_files.php">
  <p>
    <input type="file" name="file[]">
  </p>
  <p>
    <input type="file" name="file[]">
  </p>
  <p>
    <input type="file" name="file[]">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
</body>
</html>

++++++++++++++++++++++++
kopieer dit in een file met de naam

save_files.php
++++++++++++++++++++++++

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
include("multiple_file_upload_class.php");
$send_now = new multiple_upload;                //create instance
//set some values         This is the whole path to! the upload dir

$send_now->uploaddir     = SOME_SERVER . "upload_path/";//set server upload dir
$send_now->max_files     = 10;                    //set max files to upload
$send_now->max_size     = 250000;                //set max file-size
$send_now->permission     = 0777;                     //set wanted permission
$send_now->notallowed     = array("exe"."mp3");    //excluide some file-types
$send_now->show            = TRUE;                    //show errors
$send_now->files         = &$_FILES;                //get $_FILES global values

//validate on size and allowed files

$ok = $send_now->validate();
if ($ok) {
    $ok = $send_now->execute();
}

if (!$ok && $send_now->show) {
    //echo perhaps some errors
    $i_errors = count($send_now->errors);
    echo "Error report, sending files to server <br />";
    for ($i=0; $i<$i_errors;$i++) {
        echo $send_now->errors[0][$i] . " <br />";
        echo $send_now->errors[1][$i] . " <br />";
    }
}
else {
    header("Location: ". "../test_upload.htm");//redirect to something
}
?>


++++++++++++++++++++++
Kopieer dit in een file met de naam

multiple_file_upload_class.php
+++++++++++++++++++++++
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

/*
        CopyRight © ADE 2005-2006
        http://www.ade-solutions.nl
*/
/*

        Multiple File Upload (class)
        Is copyrighted, but free to use for non commercial use.
        But please let this notice intact!

        Multiple File Upload (class).php
        Simplyfied uploading file's to a webserver.
        Included are for demonstration 2 file's
        test_upload.htm : how to implement a save action
        save_files.php  : how to initiate the class
*/

//include("general_functions.php");

function HasLastSlash($content){
    $loc = $content;
    $last_slash = (substr($content,strlen($content)-1,1)=="/");
    if (!$last_slash) {
        $loc = ($content . "/");
    }

    return $loc;
}


class multiple_upload {
    var
$uploaddir;
    var
$max_files;
    var
$max_size;
    var
$permission;
    var
$show;
    var
$files;
    var
$allowed = array();
    var
$notallowed = array();
    var
$errors = array();

    function
multiple_upload()
    {

        //preset some values
        $this->uploaddir = "uploads";
        $this->max_files = 1;
        $this->max_size = 10000;
        $this->permission = 0777;
        $this->allowed = array();
        $this->notallowed = array();
        $this->show = false;
    }

    function
validate(){
        $num = count($this->files[file][name]);
        //Control for max_files
        if ($num > $this->max_files) {
            $this->errors[0][] = "To many files! max allowed=" . $this->max_files;
            return FALSE;
        }
else {
            //check for all files, SIZE and FILE_TYPE
            for ($i = 0;$i < $num; $i++) {
                //Check SIZE
                if ($this->files[file][size][$i] > $this->max_size) {
                    $this->errors[1][] = "File: " . $this->files[file][name][$i] .
                                         " size: " . $this->files[file][size][$i] .
                                         " not allowed Max=: " .
                                         (
$this->max_size/1000) . " kb";
                }

                //split file-type information (image/gif)
                $file_type = split('[/.-]', $this->files[file][type][$i]);
                //Check if file-type ALLOWED
                if (in_array($file_type[1], $this->allowed)) {
                    $this->errors[2][] = "File: " . $this->files[file][name][$i] .
                                         " type: " . $this->files[file][type][$i] .
                                         " not in list allowed";
                //else Check if file-type NOT ALLOWED
                } else if (in_array($file_type[1], $this->notallowed)) {
                    $this->errors[2][] = "File: " . $this->files[file][name][$i] .
                                         " type: " . $this->files[file][type][$i] .
                                         " not allowed";
                }
            }

            if (count($this->errors)>0) {
                return FALSE;
            }
        }

        return TRUE;
    }

    function
execute()
    {

//        echo "<html>En nu ? " . print_r($this) . "</html>";
        //Get directory

        $remdir = $this->uploaddir;
        //Add when nessecary a slash
        $dir = HasLastSlash($dir);
        //Is dir writeable

        if (!is_writable($remdir)) {
            $this->errors[0][] = "Not allowed to write to dir:" . $remdir;
            return FALSE;
        }


        $num = count($this->files[file][name]);
        //Control for max_files
        if ($num > $this->max_files) {
            $this->errors[0][] = "To many files! max allowed=" . $this->max_files;
            return FALSE;
        }
else {
            //check for all files, SIZE and FILE_TYPE
            for ($i = 0;$i < $num; $i++) {
                $filename = $this->files[file][name][$i];
                //$this->errors[0][] = $filename;
                if ( !empty( $filename)) {               // this will check if any blank field is entered
                    $add = $remdir . $filename;           // upload directory path is set
                    if(is_uploaded_file($this->files[file][tmp_name][$i]))
                    {

                        move_uploaded_file($this->files[file][tmp_name][$i], $add);
                        if (!chmod( "$add", $this->permission)) { // set permission to the file.
                              $this->errors[0][] = "Problems with copy of: " . $filename;                    }
                    }

                    /*
                    copy($this->files[file][tmp_name][$i], $add); //upload the file to the server
                    if (!chmod( "$add", $this->permission)) { // set permission to the file.
                          $this->errors[0][] = "Problems with copy of: " . $filename;                    }
                      */

                }
            }

            return TRUE;
        }
    }
}

?>


++++++++++++++++++++++
Verander in save_files.php SOME_SERVER in jouw servernaam
en pas ook de gewenste instellingen aan,
plaats dan de files op de webserver en type
++++++++++++++++++++++

Succes

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.