Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

Webserver1/Ovningar/Slutprojekt/DEBUG/

ffmpeg_testing.php
force_verify_timeout.php
reset_session.php
test_upload.php
uploads/

test_upload.php

76 lines UTF-8 Windows (CRLF)
<?php
// show all error reporting
error_reporting(-1); // Report all type of errors
ini_set('display_errors'1); // Display all errors 
ini_set('output_buffering'0); // Do not buffer outputs, write directly

var_dump($_POST);


if (isset(
$_FILES["img-input"])) {
  
$target_dir "uploads/";
  
$target_file $target_dir basename($_FILES["img-input"]["name"]);
  
  echo 
"<pre>";
  echo 
"target dir:";
  
print_r($target_dir);
  echo 
'<br>';
  echo 
"target file path:";
  
print_r($target_file);
  echo 
'<br>';
  echo 
"full target dir:";
  
var_dump(realpath($target_dir));
  echo 
'<br>';
  echo 
'Full $_FILES:';
  
print_r($_FILES);
  echo 
"</pre>";
  
  if (!
getimagesize($_FILES["img-input"]["tmp_name"])) {
    echo 
"Filen är fel format";
    throw new 
Exception("Fel format på fil.");
  };
  
  if (
file_exists($target_file)) {
    echo 
"Filen $target_file finns redan.";
    throw new 
Exception("Filen $target_file finns redan.");
  }
  
  if (!
realpath($target_dir)) {
    echo 
$target_dir " finns inte hos " dirname(__FILE__);
    throw new 
Exception($target_dir " finns inte hos " dirname(__FILE__));
  }

  
// Om större än 10MB
  
if ($_FILES["img-input"]["size"] > 10 1024 1024) {
    throw new 
Exception("Filen är för stor!");
  }
  
  
$success move_uploaded_file($_FILES["img-input"]["tmp_name"], $target_file);
  if (
$success) {
    echo 
"Filen har laddats upp till " realpath($target_dir);
  }
  else {
    echo 
"Filuppladdning misslyckades";
  }
}
else {
  echo 
"Ingen fil vald!";
}

?>

<!DOCTYPE html>
<html lang="sv">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Test</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
    <label for="img-input">Bild</label>
    <input type="file" name="img-input" id="img-input">
    <input type="submit" value="Ladda upp">
  </form>
</body>
</html>