从文件夹 PHP 中删除图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15005899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:21:44  来源:igfitidea点击:

delete image from folder PHP

php

提问by emcee22

I have a folder where I keep my images, named img/. I have a table with all of my images:

我有一个文件夹,用于保存我的图像,名为img/. 我有一张包含我所有图像的表格:

<table border="3">
    <tr>
        <td>    
            <?php
            $files = glob("img/*");
            foreach ($files as $file) {
                echo "<div class='divimages'>"; 
                echo '<img src="'.$file.'"/>';
                echo "<input type='submit' value='Delete image'/><br>";
                echo "</div>";  
            }
            ?>
        </td>
    </tr>   
</table>

How can I delete the image associated to the button with the value:"Delete image".

如何删除与具有值的按钮关联的图像:"Delete image"

回答by Chris Baker

There are a few routes. One, the most simple, would involve making that into a form; when it submits you react to POST data and delete the image using unlink

有几条路线。一个,最简单的,就是把它做成一种形式;当它提交时,您对 POST 数据做出反应并使用删除图像unlink

DISCLAIMER: This is not secure. An attacker could use this code to delete anyfile on your server. You must expand on this demonstration code to add some measure of security, otherwise you can expect bad things.

免责声明:这不安全。攻击者可以使用此代码删除您服务器上的任何文件。您必须扩展此演示代码以添加一些安全措施,否则您可能会遇到不好的事情

Each image's display markup would contain a form something like this:

每个图像的显示标记将包含如下形式:

echo '<form method="post">';
  echo '<input type="hidden" value="'.$file.'" name="delete_file" />';
  echo '<input type="submit" value="Delete image" />';
echo '</form>';

...and at at the top of that same PHP file:

...并在同一个 PHP 文件的顶部:

if (array_key_exists('delete_file', $_POST)) {
  $filename = $_POST['delete_file'];
  if (file_exists($filename)) {
    unlink($filename);
    echo 'File '.$filename.' has been deleted';
  } else {
    echo 'Could not delete '.$filename.', file does not exist';
  }
}
// existing code continues below...

You can elaborate on this by using javascript: instead of submitting the form, you could send an AJAX request. The server-side code would look rather similar to this.

您可以使用 javascript 对此进行详细说明:您可以发送 AJAX 请求,而不是提交表单。服务器端代码看起来与此非常相似。

Documentation and Related Reading

文档和相关阅读

回答by mcryan

You can delete files in PHP using the unlink()function.

您可以使用该unlink()函数删除 PHP 中的文件。

unlink('path/to/file.jpg');

回答by Wajid khan

First Check that is image exists? if yes then simply Call unlink(your file path) function to remove you file otherwise show message to the user.

首先检查图像是否存在?如果是,那么只需调用 unlink(your file path) 函数来删除您的文件,否则向用户显示消息。

              if (file_exists($filePath)) 
               {
                 unlink($filePath);
                  echo "File Successfully Delete."; 
              }
              else
              {
               echo "File does not exists"; 
              }

回答by Winston

For deleting use http://www.php.net/manual/en/function.unlink.phpHope you'll can to write logic?

删除使用http://www.php.net/manual/en/function.unlink.php希望你能写逻辑?

回答by Obaidul Haque

You can try this code. This is Simple PHP Image Deleting code from the server.

你可以试试这个代码。这是从服务器删除简单的 PHP 图像代码。

<form method="post">
<input type="text" name="photoname"> // You can type your image name here...
<input type="submit" name="submit" value="Delete">
</form>

<?php
if (isset($_POST['submit'])) 
{
$photoname = $_POST['photoname'];
if (!unlink($photoname))
  {
  echo ("Error deleting $photoname");
  }
else
  {
  echo ("Deleted $photoname");
  }
}
?>

回答by Ahsan Ahmed Rakib

<?php

    require 'database.php';

    $id = $_GET['id'];

    $image = "SELECT * FROM slider WHERE id = '$id'";
    $query = mysqli_query($connect, $image);
    $after = mysqli_fetch_assoc($query);

    if ($after['image'] != 'default.png') {
        unlink('../slider/'.$after['image']);
    }

    $delete = "DELETE FROM slider WHERE id = $id";
    $query = mysqli_query($connect, $delete);

    if ($query) {
        header('location: slider.php');
    }

?>