Logo  

CS479/579 - Web Programming II

Displaying ./code/Uploads/file-multiple.php

<?php
$mesgs = [];
if (isset($_FILES['fx'])) {

  $names = $_FILES['fx']['name'];

  foreach ($names as $idx => $name) {
    if (strstr($name, "/")) die("File had slashes in it.");

    if ($_FILES['fx']['error'][$idx] != 0) {
      switch ($_FILES['upfile']['error'][$idx]) {
        case UPLOAD_ERR_NO_FILE:
            $mesg = 'No file sent.';
        case UPLOAD_ERR_INI_SIZE:
        case UPLOAD_ERR_FORM_SIZE:
            $mesg = 'Exceeded filesize limit.';
        default:
            $mesg = 'Unknown errors.';
      }
      $mesgs[] = "File upload error (" . $_FILES['fx']['error'][$idx] . ")";
    }
    if ($_FILES['fx']['error'][$idx] == 0) {
      $dest = "/u1/h7/sbaker/public_html/cs479/code/11-02-Uploads/uploads/" . $name;
      if (move_uploaded_file($_FILES['fx']['tmp_name'][$idx], $dest)) $mesgs[] = "File ($name) was successfully uploaded.";
      else $mesgs[] = "Failed to upload $name.";
    }
  }
}
done:
?>
<!DOCTYPE html>
<html>
<head>
 <title> File uploads </title>
 <meta charset='utf-8'>
</head>
<body>
<h1><?php foreach ($mesgs as $mesg) { echo $mesg . "<br>"; } ?></h1>

<form method=POST enctype="multipart/form-data">
 <input type=file name=fx[] multiple><br>
 <input type=submit>
</form>

</body>
</html>