Search code examples
phphtml-tableunlink

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 but something in this code is not working. Any help?

<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=up/<?php echo $key?>' id="button">Delete now</a> 
} 
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);
}
?>    

Solution

  • 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 ;)