PHP - Upload

Aus Wikizone
Wechseln zu: Navigation, Suche

Siehe auch PHP - Upload Formular

Multiple File Upload mit HTML5[Bearbeiten]

Einfaches Beispiel. Kann mit Typvalidierungen (s.u. und Link oben) verbessert werden. Wichtig sind die Attribute im Input Feld und der Loop mit denen die Files abgearbeitet werden. Sonst alles wie beim normalen Upload. Schick wird es dann mit JavaScript und hübscherem Drag & Drop

<html lang="en">
<head>
  <meta http-equiv="content-type" content="text/html, charset=utf-8" />
</head>
<body>
  <h2>Multiple Files Upload mit HTML5 und PHP</h2>
  <p> Wir benötigen das Attribut <code>enctype="multipart/form-data"</code> im <code>form</code> Tag</p>
  <form action="" enctype="multipart/form-data" method="post">
    <div>
       <p>Das Input Feld enthält das Attribut <code>multiple="multiple"</code> und erlaubt damit mehrere Dateien hochzuladen.</p>
       <label for='upload'>Füge eine oder mehrere Dateien hinzu.</label><br>
       <input id='upload' name="upload[]" type="file" multiple="multiple" />
   </div>
   <p><input type="submit" name="submit" value="Submit"></p>
  </form>

  <?php
  // Auswertung
  $messages = array();;
  $upload_ok = 1;
  $strDate=date("Y-m-d_H-i-s");

  /***** CONFIGURATION PART  *****/
  $actualPath = dirname($_SERVER["SCRIPT_FILENAME"]);
  $uploadFolder = $actualPath.'/uploads/';
  $maxAllowedFileSize = 10000; // size in KB
  $allowedExtensions = array("jpg", "jpeg", "gif", "bmp", "png");
  //FILE INFOS
  /*
  echo('$_FILES Variable');
  var_dump($_FILES);
  echo('$_SERVER Variable');
  var_dump($_SERVER);
  */
  if(isset($_POST['submit'])){// Form gesendet
    if(count($_FILES['upload']['name']) > 0){ // Gibt es hochgeladene Dateien ?
      for($i=0; $i < count($_FILES['upload']['name']); $i++) { // LOOP durch alle temporären Dateien
        $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; // Pfad zur temporären Datei auslesen
        if($tmpFilePath != ""){ // Pfad wirklich vorhanden
          $fileShortname = $_FILES['upload']['name'][$i]; // Dateiname merken
          $filePath = $uploadFolder . $strDate . '_' . $fileName;//save the url and the file
          if(move_uploaded_file($tmpFilePath, $filePath)) { // File abspeichern
            //insert into db
            //use $shortname for the filename
            //use $filePath for the relative url to the file
          }
        }
      }
    }
  }
  ?>
</body>
</html>

Einfacher Bildupload[Bearbeiten]

index.php

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>File Upload</title>
</head>

<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
  <div>
    <label for="file">Datei auswählen</label>
    <input type="file" name="file" id="file">
  </div>
  <div>
    <input type="submit" value="Submit" id="submit" />
  </div>
</form>

</body>
</html>

upload.php

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["file"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG and GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
	var_dump($_FILES);
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>