Created
October 23, 2018 12:15
-
-
Save Florencelg/d066d3087a8829de9aeaab8e96c58253 to your computer and use it in GitHub Desktop.
Quête php: laisse pas traîner ton File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<! Doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Multiple File Upload</title> | |
</head> | |
<body> | |
<?php | |
$authorisedExtension = ['png', 'gif', 'jpg']; | |
$maxSize = 1048576; | |
$files=[]; | |
if(isset($_POST['submit'])){ | |
if(count($_FILES['upload']['name']) > 0){ | |
//Loop through each file | |
for($i=0; $i<count($_FILES['upload']['name']); $i++) { | |
$extension = pathinfo($_FILES['upload']['name'][$i], PATHINFO_EXTENSION); | |
if (in_array($extension, $authorisedExtension)) { | |
if($_FILES['upload']['size'][$i] < $maxSize){ | |
//Get the temp file path | |
$tmpFilePath = $_FILES['upload']['tmp_name'][$i]; | |
//Make sure we have a filepath | |
if($tmpFilePath != ""){ | |
//save the filename | |
$shortname = $_FILES['upload']['name'][$i]; | |
//save the url and the file | |
$filePath = "uploaded/" .'image'.uniqid(). ".".$extension; | |
//Upload the file into the temp dir | |
if(move_uploaded_file($tmpFilePath, $filePath)) { | |
$files[] = $shortname; | |
//insert into db | |
//use $shortname for the filename | |
//use $filePath for the relative url to the file | |
} | |
} | |
}else{ | |
echo "le fichier est trop lourd"; | |
} | |
}else{ | |
echo "mauvais type de fichier"; | |
} | |
} | |
} | |
//show success message | |
echo "<h1>Uploaded:</h1>"; | |
if(is_array($files)){ | |
echo "<ul>"; | |
foreach($files as $file){ | |
echo "<li>$file</li>"; | |
} | |
echo "</ul>"; | |
} | |
} | |
$it = new FilesystemIterator('uploaded/'); | |
foreach ($it as $fileinfo) {?> | |
<form method="post" action="index.php"> | |
<img src="../uploaded/<?php echo $fileinfo->getFilename() ?>" alt="..." class="img-thumbnail"> <br /> | |
<button type="submit" value="<?php echo $fileinfo->getFilename() ?>" name="delete">Delete</button> | |
</form> | |
<?php | |
echo $fileinfo->getFilename() . "\n"; | |
} ?> | |
<?php | |
if (isset($_POST['delete'])){ | |
$filePath='uploaded/'.$_POST['delete']; | |
if(file_exists ( $filePath)){ | |
unlink($filePath); | |
} | |
} | |
?> | |
<h1>Laisse pas traîner ton File</h1> | |
<form action="" enctype="multipart/form-data" method="post"> | |
<div> | |
<label for='upload'>Add Attachments:</label> | |
<input id='upload' name="upload[]" type="file" multiple="multiple"/> | |
</div> | |
<p><input type="submit" name="submit" value="Submit"></p> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment