I am uploading images in the "images" folder on my web server and I want all uploaded images to be visible in a table in index.html page with a delete button for each uploaded image so that i can be able to delete any image i want to delete. I don't want anything to do with database and i will be the one uploading the images from my admin. I only want a simple scripts that will do this job, thanks everyone
index.php
<form name="upload" action="upload.php" method="POST" enctype="multipart/form-data">
Select image to upload: <input type="file" name="image">
<input type="submit" name="upload" value="upload">
</form>
upload.php
<?php
$uploaddir = 'images/';
$uploadfile = $uploaddir . basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
echo "Image succesfully uploaded.";
} else {
echo "Image uploading failed.";
}
?>
First of all i have printed all the images which are in my images folder in a table with a delete button next to each image
filePrint.php
<?php
$files = glob("images/*.*");
echo "<table>";
for ($i=0; $i<count($files); $i++)
{
$image = $files[$i];
$supported_file = array(
'gif',
'jpg',
'jpeg',
'png'
);
$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
echo '<tr>';
echo "<form method='post' action='deleteFile.php?img=".$image."'>";
echo '<td><img width="100px" height="100px" src="'.$image .'" alt="Random image" /></td>';
echo '<td><input type="submit" name="delete" value="DELETE"/></td>';
echo "</file>";
echo '</tr>';
} else {
continue;
}
}
echo "</table>";
?>
I have put the action of form as deleteFile.php?img=$image
$image is printed using the for loop which gets all filenames with extension I passed $image as query string and then received it on the other page and used the unlink function to delete the file
deleteFile.php
<?php
if(isset($_POST['delete']))
{
$imgsrc = $_GET['img'];
if(file_exists($imgsrc))
{
unlink($imgsrc);
echo "SUCCESSFULLY DELETED ".$imgsrc;
}
else
{
echo "PROBLEM IN INNER IF";
}
}
?>