简体   繁体   English

从php中的本地服务器文件夹中删除图像

[英]Delete the image from the local server folder in php

I wrote one php script in these script i delete all the images data from table but i want to delete also that images which are deleted from table from the my image folder.Here i delete the image using sql self join.What i do to delete the images from my folder which are deleted from my datbase. 我在这些脚本中编写了一个PHP脚本,我从表中删除了所有图像数据,但我也想从我的图像文件夹中删除从表中删除的图像。在这里,我使用sql self join删除了图像。从我的数据库中删除的文件夹中的图像。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cribsuite";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "delete t1
from rr2s4_cma_images t1 JOIN rr2s4_cma_images t2
on t1.matrix_unique_id = t2.matrix_unique_id
and t1.ordering > t2.ordering";
//echo $sql;
$vin = mysql_query($sql);
//echo $sql;
if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

$conn->close();


[![enter image description here][1]][1]

?>

You will want to build an array of image filenames to delete from your server. 您将要构建一个图像文件名数组,以从服务器中删除。 To do that, you will want to do the following: 为此,您将需要执行以下操作:

  1. Run a SELECT query using the same WHERE statements you have here. 使用与此处相同的WHERE语句运行SELECT查询。

  2. Loop through the results of that SELECT query and add each image filename to an array of filenames. 循环浏览该SELECT查询的结果,并将每个图像文件名添加到文件名数组中。 I would recommend using the unique id of the database record as the key for the each respective element in the array. 我建议使用数据库记录的唯一ID作为数组中各个元素的键。

  3. Once you have that array built, you will want to loop through that array and delete the record from the database and then the file from the server. 构建完该阵列后,您将需要遍历该阵列并从数据库中删除记录,然后从服务器中删除文件。 You can use the unlink() function that is native to PHP to do the file deletion. 您可以使用PHP固有的unlink()函数删除文件。

NOTE : If you are wanting to make this a script that is executed on a scheduled basis, I would recommend keep track any file that does not get deleted or causes any errors or warnings. 注意 :如果要使此脚本成为按计划执行的脚本,我建议您跟踪任何未被删除或导致任何错误或警告的文件。 That would help troubleshoot any issue that may occur in the future. 这将有助于解决将来可能发生的任何问题。

I hope that helps. 希望对您有所帮助。 Let me know if you need any clarifications on any of this. 让我知道您是否需要任何澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM