Scripts
Multiple File Upload Class
Een simpele oplossing voor het uploaden van meerdere files. Het bestaat uit 3 onderdelen 1: test_upload.htm = het demo file 2: save_files.php = class initialiserings voorbeeld 3: multiple_file_upload_class.php = de werkelijke class Wel opletten dat de rechten van de uploaddir naar schrijfrechten gezet zijn, als je dat niet weet dan het aan de webmaster vragen.
multiple-file-upload-class
++++++++++++++++++++++
Kopieer alles in een file met de naam
test_upload.htm
++++++++++++++++++++++
[code]
<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>
[/code]
++++++++++++++++++++++++
kopieer dit in een file met de naam
save_files.php
++++++++++++++++++++++++
<?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
+++++++++++++++++++++++
<?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
Reacties
0