Skip to content Skip to sidebar Skip to footer

Add To An Uploaded Images Table The Choice Of Deleting One Of The Images

I have a page where I can upload some images, see the list of images loaded in a table. I want to add the chance to delete one of the images in the list from the server. I tried bu

Solution 1:

Mistakes that have done :

  1. Not closing the loop }} at the end

  2. Not giving the proper file path $dir.$files{$i} in the anchor tag.

Here's the eval

Here's the code :

<html>
<body> 
<form action="" method="post" enctype="multipart/form-data">
    Select a photo to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Load" name="submit">
</form>

<?php 
$dir = 'up/'; 
$files = scandir($dir);
$maxnum = count($files); 
?> 

<table class="thumbnail"> 
<?php $i = 2; 
for ($j = 0; $j < $maxnum; $j++) { 
echo '<tr>'; 
$k = $i + 5; 
for ($i; $i < $k; $i++) { 
If ($i == $maxnum) { break; } 
echo '<td><br><a href="'.$dir.$files{$i}.'"><img src="'.$dir.$files{$i}.'" height="50" width="50"></a> </td>';
 if (isset($_GET['delete'])) 
    {
        unlink($_GET['delete']);

        $_SESSION['delete'] = $_GET['delete'];
        unset($_GET['delete']);
        $url = $_SERVER['SCRIPT_NAME'].http_build_query($_GET);
        header("Refresh:0; url=".$url);
    }
?> 
   <a href='index.php?delete=<?php echo $dir.$files{$i}?>' id="button">Delete now</a> 


</table> 
</div>
</body>
</html>
<?php
if (isset($_POST['submit'])) 
{
echo '<center><h1>succesfully loaded!</h1></center>';
$structure = 'up/';
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
}}
?>

Note :

The first anchor tag might look at left aligned. It's just a design issue ;)


Solution 2:

I've found two problems: you forgot a <?php tag and your delete link was wrong

So the code:

<html>
<body> 
<form action="" method="post" enctype="multipart/form-data">
    Select a photo to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Load" name="submit">
</form>

<?php 
$dir = 'up/'; 
$files = scandir($dir);
$maxnum = count($files); 
?> 

<table class="thumbnail"> 
<?php $i = 2; 
for ($j = 0; $j < $maxnum; $j++) { 
echo '<tr>'; 
$k = $i + 5; 
for ($i; $i < $k; $i++) { 
If ($i == $maxnum) { break; } 
echo '<td><a href="'.$dir.$files{$i}.'"><img src="'.$dir.$files{$i}.'"></a> </td>';
 if (isset($_GET['delete'])) 
    {
        unlink($_GET['delete']);

        $_SESSION['delete'] = $_GET['delete'];
        unset($_GET['delete']);
        $url = $_SERVER['SCRIPT_NAME'].http_build_query($_GET);
        header("Refresh:0; url=".$url);
    }
?>

   <a href='index.php?delete=<?php echo $dir.$files{$i}?>' id="button">Delete now</a>
<?php //<--you forgot this
} 
echo "</tr>"; 
} ?> 
</table> 
</div>
</body>
</html>
<?php
if (isset($_POST['submit'])) 
{
echo '<center><h1>succesfully loaded!</h1></center>';
$structure = 'up/';
$target_file = $structure.basename($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}
?>  

Post a Comment for "Add To An Uploaded Images Table The Choice Of Deleting One Of The Images"