添加到上传的图片table删除其中一张图片的选择

Add to an uploaded images table the choice of deleting one of the images

我有一个可以上传一些图片的页面,请参阅 table 中加载的图片列表。我想增加从服务器删除列表中的图像之一的机会。我试过了,但这段代码中的某些内容不起作用。有什么帮助吗?

<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);
}
?>    

曾经犯过的错误:

  1. 最后没有关闭循环}}

  2. 没有在锚标签中给出正确的文件路径$dir.$files{$i}

这是eval

代码如下:

<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);
}
}}
?>

注:

第一个锚标记可能看起来是左对齐的。这只是一个设计问题 ;)

我发现了两个问题:您忘记了 <?php 标签 你的删除 link 是错误的

所以代码:

<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);
}
?>