Das PHP Script Verzeichnis
Werbung

Dieses Script- Beispiel zeigt wie man mehrere Bilder mittels PHP gleichzeitig auf einen Server hochladen kann.

multi_upload.php

<html>
<head>
</head>
<title> Dynamic Image Uploading :: Automatic Create File Uploading Field</title>
<head>
<script type="text/javascript" src="js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.custom.min.js"></script>
<script type="text/javascript"> 

<?php
$(function(){

$("a:[id=createNew]").bind("click",function(){

var CounterLimit = 10;
var LastDivId = $("div:[id=FileUploadDiv]:last").attr("DivValue");
var appandingDiv = $("div:[id=FileUploadDiv][DivValue="+LastDivId+"]");
var NewDivId = eval(LastDivId)+1;
if(CounterLimit>NewDivId){
   var NewUploadDiv = "<div align='left' id='FileUploadDiv' DivValue='"+NewDivId+"' style='display: none'>   "+"<input name='filename"+NewDivId+"' type='file' id='filename'>"+"</div>";
var UploadDivCounter =eval($("#counter").val())+1;
$("#counter").val(UploadDivCounter);
$(NewUploadDiv).clone().show().appendTo(appandingDiv);

}
else
{
var AlertMessage= "You Can upload maximum "+CounterLimit+" Files!";
alert(AlertMessage);
} 
  return false;
});
});
?>

</script>
<style type="text/css">
div#upload_box_wrapper{width:300px;text-align:center;border:3px solid lightblue;float: left;font-family: verdana; font-size: 12px;padding: 5px 5px 5px 5px;background:url(./images/background.jpg) no-repeat; color:white;}
div#upload_box_wrapper #upload_title_text{text-align:left;width: 100%;display: block;border: 0px solid green; margin-bottom: 5px;}
div#upload_box_wrapper #FileUploadDiv{display: block;width: 98%;border: 0px solid green; text-align: left;padding: 0px 0px 0px 0px;}
div#upload_box_wrapper p.add-new-upload-box{float: left;width: 100%;text-align: right;}
div#upload_box_wrapper a.add-new-upload-link{font-weight: bold;font-size: 13px;padding-right: 3px;text-decoration: none; color: yellow;}
div#upload_box_wrapper .upload-button{border: 0px solid lightgreen;height:30px; width:120px;cursor: pointer;background:url(./images/upload_button.png) no-repeat center left; color: white;}

</style>
</head>
<body>

<form method='post' action='multi_upload_now.php' enctype='multipart/form-data'>
<div id="upload_box_wrapper">
<div id="upload_title_text">Upload Image:</div>
<div id="FileUploadDiv" DivValue="0">
<input name='filename0' type='file' id='filename'>
</div>

<p class="add-new-upload-box">
<a href="#" id="createNew" class="add-new-upload-link">Add More </a>
</p>
<input type="hidden" value="0" id="counter" name="counter">

<div align='left'>
Upload Image:&nbsp;<input type='submit' name='submit' value='Upload Now!' class="upload-button">
</div>

</div>
</form>
</body>
</html>

multi_upload_now.php

<a href="multi_upload.php">Upload Again</a>

<?php
require_once 'upload_config.php';

  if($clear_folder_before_upload){
  $mydirectory = myUploadDir();
  EmptyDir($mydirectory);
}

  $uploaded_file_counter=0;
  $UploadLimit = $_POST['counter'];

  for($i=0;$i<=$UploadLimit;$i++){
  $file_tag='filename'.$i;
  $filename=$_FILES[$file_tag]['name'];

  if($filename!=null) {
  $rand=time();
  $str="$rand$filename";

// set folder name in here.
  $filedir= myUploadDir();

//change the string format.
  $string= $filedir.$str;
  $patterns[0] = "/ /";
  $patterns[1] = "/ /";
  $patterns[1] = "/ /";
  $replacements[1] = "_";
  $dirname=strtolower(preg_replace($patterns, $replacements, $string));

//end of changing string format

//checking the permitted file types

if($check_file_extentions) {
  $allowedExtensions = allowedfiles();
  foreach ($_FILES as $file) {
  if ($file['tmp_name'] > '') {
  if (!in_array(end(explode(".",
  strtolower($file['name']))),
  $allowedExtensions)) {
  $fileUploadPermission=0;
  }
  else
  {
   $fileUploadPermission=1;
}
}
}
}
else{
$fileUploadPermission=1;
}
//end of checking the permitted file types
   if($fileUploadPermission){
   if(move_uploaded_file($_FILES[$file_tag]['tmp_name'],$dirname)) {
   echo "<p>";
   echo "<img src='$dirname'>";
   echo "</p>";
   $uploaded_file_counter+=1;
   }
  }
 }
}

  if($uploaded_file_counter==0){
   echo "<br /> <b style='font-weight:bold;color:red'>Opss! Please select an image file<b>";
    }else{
    echo "<br /> <b>You request ".$i." image files to upload and ".$uploaded_file_counter." files uploaded    sucessfully</b>";
  }
?>

Quelle: PHPSnips