1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
<?php
$connect = mysqli_connect("localhost", "root", "", "test");
if(isset($_POST["insert"]))
{
$filetmp = $_FILES["image"]["tmp_name"];
$filename = $_FILES["image"]["name"];
$filetype= $_FILES["image"]["type"];
$filepath = "Projets/".$_GET['name']."/".$filename;
$folder1 = "Projets/".$_GET['name'];
//Get a list of all of the file names in the folder.
$files = glob($folder1 . '/*');
//Loop through the file list.
foreach($files as $file){
//Make sure that this is a file and not a directory.
if(is_file($file)){
//Use the unlink function to delete the file.
unlink($file);
}
}
move_uploaded_file($filetmp,$filepath);
$query ="UPDATE projects SET
img_name = '$filename',
img_path = '$filepath',
img_type = '$filetype'
WHERE name = '".$_GET["name"]."'";
if(mysqli_query($connect, $query))
{
echo '<script>alert("La photo a bien été mise à jour")</script>';
header("Location: listProjects.php?success=1");
exit();
}
}
?>
<!-- Mise en Page HTML-->
<?php
$dir_path = "Projets/".$_GET['name']."/";
$extensions_array = array('jpg','png','jpeg');
if(is_dir($dir_path))
{
$files = scandir($dir_path);
for($i = 0; $i < count($files); $i++)
{
if($files[$i] !='.' && $files[$i] !='..')
{
// get file extension
$file = pathinfo($files[$i]);
$extension = $file['extension'];
// echo "File Extension-> $extension<br>";
// check file extension
if(in_array($extension, $extensions_array))
{ ?>
<tr>
<td> <?php
// show image
echo "<img src='$dir_path$files[$i]' class=img-thumnail style='width:500px;height:500px;'><br>"; ?>
</td>
</tr>
<?php
}
}
}
}
?>
<script>
$(document).ready(function(){
$('#insert').click(function(){
var image_name = $('#image').val();
if(image_name == '')
{
alert("Veuillez selectionner une image");
return false;
}
else
{
var extension = $('#image').val().split('.').pop().toLowerCase();
if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1)
{
alert('Veuiillez selectionner un format valide');
$('#image').val('');
return false;
}
}
});
});
</script> |
Partager