添加到上传的图像表中可以选择删除其中一个图像

时间:2015-07-05 21:18:19

标签: php html-table unlink

我有一个页面,我可以上传一些图像,查看表格中加载的图像列表。我想添加从服务器删除列表中的一个图像的机会。我试过但是这段代码中的某些东西不起作用。有帮助吗?

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

2 个答案:

答案 0 :(得分:1)

已完成的错误:

  1. 未在结尾处关闭循环}}

  2. 未在锚标记中提供正确的文件路径$dir.$files{$i}

  3. 这是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);
    }
    }}
    ?>
    

    注意:

    第一个锚标签可能会看左对齐。这只是一个设计问题;)

答案 1 :(得分:0)

我发现了两个问题:您忘记了<?php标记 并且您的删除链接错误

所以代码:

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