meerdere bestanden tegelijk uploaden en status weergeven

Status
Niet open voor verdere reacties.

Tom25

Gebruiker
Lid geworden
23 mrt 2008
Berichten
119
Hoihoi,

Ik ben bezig met een projectje om mp3'tjes te uploaden. Ik heb nu een goed upload script:

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <title>muziek.servertom.nl</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta name="generator" content="handmade" />
	<style type="text/css">
	<!--
		body {
			font-family: Arial, Helvetica, sans-serif;
			font-size: 14px;
			background-color: #DDDDDD;
		}
		.cnt {
			text-align: center;
		}
		.cnt_welcome {
			font-size: 16px;
			font-weight: bold;
			text-align: center;
		}
		.cnt_powered {
			font-size: 14px;
			font-weight: bold;
			text-align: center;
		}
		.cnt_small {
			font-size: 12px;
			text-align: center;
			padding-top: 50px;
		}
		.head_line {
			background-color: #BBBBBB;
		}
		.main_table {
			border: solid 1px #9D9992;
			font-size: 13px;
		}
		h4 {
			font-size: 12px;
			color: #DD0000;
			text-align: center;
		}
		.button {
			border: 1px solid #555555;
			font-weight: bold;
		}
.stijl1 {
	font-size: 18px;
	font-weight: bold;
}
.stijl2 {
	font-family: "Courier New", Courier, monospace
}
-->
    </style>
</head>

<body>
<?
include ("config.php");

function path_options()
{
    global $upload_dirs;
    $option = "";
    foreach ($upload_dirs as $path => $pinfo) {
        $option .= '<option value="' . $path . '">' . $pinfo["name"] . '</option>';
    }
    return $option;
}

function check_vals()
{
    global $upload_dirs, $err;
    if (!ini_get("file_uploads")) {
        $err .= "HTTP file uploading is blocked in php configuration file (php.ini). Please, contact to server administrator.";
        return 0;
    }
    $pos = strpos(ini_get("disable_functions"), "move_uploaded_file");
    if ($pos !== false) {
        $err .= "PHP function move_uploaded_file is blocked in php configuration file (php.ini). Please, contact to server administrator.";
        return 0;
    }
    if (!isset($_POST["path"]) || (strlen($_POST["path"]) == 0)) {
        $err .= "Please fill out path";
        return 0;
    }
    if (!isset($upload_dirs[$_POST["path"]])) {
        $err .= "Incorrect path";
        return 0;
    }
    if (!isset($_POST["pwd"]) || (strlen($_POST["pwd"]) == 0)) {
        $err .= "Please fill out password";
        return 0;
    } elseif ($_POST["pwd"] != $upload_dirs[$_POST["path"]]["password"]) {
        $err .= "The upload password is incorrect";
        return 0;
    }
    if (!isset($_FILES["userfile"])) {
        $err .= "Empty file";
        return 0;
    } elseif (!is_uploaded_file($_FILES['userfile']['tmp_name'])) {
        $err .= "Empty file";
        return 0;
    }
    return 1;
}
$extentie = substr($_FILES['userfile']['name'], -3);
$err = "";
$status = 0;

for ($i = 0; $i < count($allow); $i++) {
    if ($extentie == $allow[$i]) {
        $extentie_check = "ok";
        $i = count($allow) + 5; // om loop te beindigen
    }
}

if (isset($_POST["upload"])) {
    if (check_vals()) {
        if (filesize($_FILES["userfile"]["tmp_name"]) > $max_file_size)
            $err .= "Ons limiet is: $max_file_size bytes";
        elseif (!isset($extentie_check)) {
            $err .= "Het moet een mp3'tje zijn.";
        } else {
            if (move_uploaded_file($_FILES["userfile"]["tmp_name"], $upload_dirs[$_POST["path"]]["dir"] .
                $_FILES["userfile"]["name"])) {
                $status = 1;
            } else
                $err .= "Er zijn wat problemen!";
        }
    }
}

if (!$status) {
    if (strlen($err) > 0)
        echo "<h4>$err</h4>";
} else {
    echo "<h4>&quot;" . $_FILES["userfile"]["name"] .
        "&quot; is goed aangekomen.</h4>"
		;
}
?>
<p class="cnt_welcome">&nbsp;</p>
<p class="cnt">&nbsp;</p>
<p class="cnt">&nbsp;</p>
<br />
<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?= $max_file_size ?>" />
<table class="main_table" align="center">
  <tr>
    <td colspan="2" class="head_line"><div align="center">
      <p class="stijl1 stijl2">Upload hier je hardstyle..</p>
      </div></td>
  </tr>
  <tr>
    <td width="39"><div align="right">Genre:</div></td>
    <td width="341"><select name="path"><?= path_options() ?></select></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name="pwd" type="hidden" style="width: 217px;" value="servertom" /></td>
  </tr>
  <tr>
    <td><div align="right">Track:</div></td>
    <td><input type="file" name="userfile" style="width: 222px;" /></td>
  </tr>
  <tr>
    <td colspan="2" align="right"><div align="center">
      <p>
        <input type="submit" name="upload" value="Upload" class="button" />
        </p>
      </div></td>
  </tr>
</table>
<p align="center"><span class="cnt">Maximale grootte:
    <?= $max_file_size / 1048576 ?>
Mb.</span></p>
</form>
</body>
</html>

Dat is de index.

En dit is de config:

PHP:
<?

$allow[0] = "mp3";
$allow[1] = "-";
$allow[2] = "-";

$upload_dirs = array(
  "hardstyle" => array(
      "dir"     =>"uploads/hardstyle/",
      "name"    =>"Hardstyle",
      "password"=>"servertom",
  ),
  "happyhardcore" => array(
      "dir"     =>"uploads/happyhardcore/",
      "name"    =>"Happy Hardcore",
      "password"=>"servertom",
  ),

);

$max_file_size = 25*1048576; //max file upload size (bytes)
?>

Nu was het plan dat mensen meerdere bestanden tegelijk kunnen uploaden. Dus iedere keer dat je een bestand hebt gekozen dat er weer een nieuwe browse bijkomt tot 25 keer of zo. En dat je dan een status balk krijgt te zien hoeveel er al zijn aangekomen. Bijvoorbeeld: "Status: 15/21". Maar dit laatste is denk ik niet al te makkelijk zelfs niet voor de ervaren helpmij php deskundigen. Dus met het eerste ben ik ook al tevreden :p

Dus als iemand wil helpen, bedankt!! :thumb:


Gr,
Tom
 
Persoonlijk zou ik meerdere vensters lelijk vinden.

Ik zou kiezen voor een ajax oplossing.
daar kun je met een van de vele gratis ajax kits de status opvragen van een upload.

Die status van iedere update kun je vervolgens weer verder in andere (client side) code gebruiken om je overzicht te laten zien.
 
Hij bedoeld niet een nieuwe browser, maar een nieuw browse veld. Dat is wel een goed idee.
 
Jah, dat ajax lijkt mij wel wat. Ik heb er al wat van gezien en ziet er goed uit!!
Dus dikke kans dat het dat wordt. :D
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan