有一个上传脚本我可以删除一个图像我必须在表格的输入框中手动编写它的名字。我直接从我服务器的文件夹中读取图像的名称。我想通过单击要删除的每个图像的图像或名称来复制我要删除的图像的名称。解释它并不容易,我会告诉你代码:
EDIT2: It'working:
<?php
if ( $_SERVER["REQUEST_METHOD"] == 'GET' ) {
if (isset($_GET['name']) && $_GET['name'] != '') {
$img_file = $_GET['name'];
if($img_file){
unlink("img/$img_file");
header('Location: index.php');
}
}
}
?>
<?php
if(isset($_POST['upload_img'])){
$file_name = $_FILES['image']['name'];
$file_type = $_FILES['image']['type'];
$file_size = $_FILES['image']['size'];
$file_tmp_name = $_FILES['image']['tmp_name'];
if($file_name){
move_uploaded_file($file_tmp_name,"img/$file_name");
}
}
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<label>upload image </label><br>
<input type="file" name="image"><br>
<input type="submit" value="Upload Image" name="upload_img">
</form>
<?php
$folder = "img/";
if(is_dir($folder)){
if($handle = opendir($folder)){
while(($file=readdir($handle)) != false){
if($file==='.' || $file==='..') continue;
echo '<a href="index.php?name='.$file.'">';
echo '<img src="img/'.$file.'" width="150" height="150" alt="">';
echo '</a>';
}
closedir($handle);
}
}
echo '<br>'.$file_names;
?>
</body>
答案 0 :(得分:0)
您可以将img
标记放在a
标记内,并附带指向删除文件的脚本的链接,例如:
echo <<< EOF
<a href="deleteFile.php?fileToDelete=$file"> <img src="img/$file" width="150" height="150" alt=""> </a>
EOF;
然后创建一个名为deleteFile.php
if(isset($_GET['fileToDelete')){
//NOTE: Make SURE you filter and restrict the content of $_GET['fileToDelete'), or may get all the files removed...
//remove the file here
}
答案 1 :(得分:0)
您需要将img标记包装在锚标记中,如此
假设您当前的脚本名为mypics.php
,您可以执行此操作。
echo '<a href="mypics.php?name="' . $file . '">';
echo '<img src="img/'.$file.'" width="150" height="150" alt="">';
echo '</a>';
现在在当前脚本的顶部添加
<?php
// picture clicks will be returned as GET requests
if ( $_SERVER["REQUEST_METHOD"] == 'GET' ) {
if (isset($_GET['name']) && $_GET['name'] != '') {
// do any sanity checks that you are only
// deleting a sensible file name etc
// delete the file
}
// your existing script follows, which should re-paint the page
// without the deleted image
您现在可以删除用于输入名称和以此方式处理删除的代码