如何使用旁边的删除按钮显示上传的文件

时间:2019-11-01 03:05:07

标签: php display

我在php中有使用删除按钮显示上传图像的代码。这些代码在我可以显示图像并删除它们的地方效果很好。但是问题出在页面底部的删除按钮。我正在考虑在每个上传的图像上放置按钮。 谢谢有人能帮助我。.

先谢谢了。

<!-- Uploaded images -->
<?php
$dir_path = "../../img/gallery/";
$extensions_array = array('jpg','png','jpeg');
$numCol = 2;
if(is_dir($dir_path))
{
$files = scandir($dir_path);
for($i = 0; $i < count($files); $i++)
{
$n = 0;
echo '<table>';
echo '<tr>';
if($files[$i] !='.' && $files[$i] !='..')
{
$n ++;

// get file name
echo "<td>File Name: $files[$i]</td><br>";
// get file extension
$file = pathinfo($files[$i]);
$extension = $file['extension'];

// show image
echo "<td><img src='$dir_path$files[$i]' style='width:0px;height:80px; 
padding:3px'></td>";

if($n % $numCol == 2) echo "</tr><tr>";
}
echo '</tr>';
echo '</table>';
}
}
?>

<?php
$path = "../../img/gallery";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{   
unlink($path . "/" . $file) or die("Failed to <strong 
class='highlight'>delete</strong> file");
}
header("location: " . $_SERVER['REQUEST_URI']); //redirect after deleting 
files so the user can refresh without that resending post info message
}
?>

<form name="form1" method="post">
<?php
$path = "../../img/gallery";
$dir_handle = @opendir($path) or die("Unable to open folder");
while (false !== ($file = readdir($dir_handle))) 
{

if($file == ".")
continue;
if($file == "..")
continue;
echo "<input type='CHECKBOX' name='file[]' value='$file'>";
}
closedir($dir_handle);
?>
<br>
<input class="btn btn-primary" type="submit" name="Delete" value="Delete">
</form>
<!-- End of image uploaded Section -->
</div>

0 个答案:

没有答案