Skip to content

Instantly share code, notes, and snippets.

@Karnak19
Created October 24, 2018 09:20
Show Gist options
  • Select an option

  • Save Karnak19/6231dbb2c4c1a57845985e7cf50d2695 to your computer and use it in GitHub Desktop.

Select an option

Save Karnak19/6231dbb2c4c1a57845985e7cf50d2695 to your computer and use it in GitHub Desktop.
<?php
$acceptedType = array('image/gif','image/jpeg','image/png');
if(isset($_POST['submit']))
{
if(count($_FILES['pictures']['name']) > 0)
{
//Loop through each file
for($i=0; $i<count($_FILES['pictures']['name']); $i++)
{
//Get the temp file path
$tmpFilePath = $_FILES['pictures']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != "")
{
//save the filename
$extension = pathinfo($_FILES['pictures']['name'][$i], PATHINFO_EXTENSION);
$fileName = "image".uniqid() . '.' .$extension;
//save the url and the file
$filePath = "files/" . $fileName;
$fileType = $_FILES['pictures']['type'][$i];
if(filesize($tmpFilePath)<1000000 && in_array($fileType,$acceptedType))
{
//Upload the file into the temp dir
move_uploaded_file($tmpFilePath, $filePath);
}
}
}
}
}
if(isset($_POST['delete']))
{
if(file_exists($_POST['pixToDelete']))
{
unlink($_POST['pixToDelete']);
}
}
?>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Image galery</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pictures[]" multiple="multiple" />
<input type="submit" name="submit" value="Send" />
</form>
<div>
<?php
$images = new FilesystemIterator("files/");
foreach ($images as $image) { ?>
<div class="card m-2" style="width: 18rem;">
<img class="card-img-top" src="<?php echo 'files/'.$image->getFilename();?>" alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><?php echo $image->getFilename(); ?></h5>
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="pixToDelete" value=<?php echo 'files/'.$image->getFilename();?> />
<input type="submit" name="delete" value="Delete" />
</form>
</div>
</div>
<?php
}
?>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment