[英]How to delete all files in all sub-folders except those whose filename is 'whatever.jpg' in PHP?
[英]Delete all except those specified
我有一個程序可以將條目發送到我的php腳本,然后將其添加到數據庫中,並且如果已經存在則進行更新
該程序發送了大約3000個條目,並且php腳本將id保存到文本文件中,在發送完所有條目之后,php腳本應該從數據庫中刪除所有條目,但那些id在文本文件中的條目除外。
目前,這是我的代碼:
if($finished == "yes")
{
if(file_exists("completed.txt"))
{
$completed = file("completed.txt");
unlink("completed.txt");
$product = database::query("select * from " . DB_TABLE_PRODUCTS . ";");
while($row = database::fetch($product))
{
if(!in_array($row["ewe"], $completed))
{
if($row["ewe"] != "" && $row["ewe"] != null)
{
database::query("delete from " . DB_TABLE_PRODUCTS . " where id = '" . $row["id"] . "';");
database::query("delete from " . DB_TABLE_PRODUCTS_TO_CATEGORIES . " where product_id = '" . $row["id"] . "';");
database::query("delete from " . DB_TABLE_PRODUCTS_INFO . " where product_id = '" . $row["id"] . "';");
database::query("delete from " . DB_TABLE_PRODUCTS_PRICES . " where product_id = '" . $row["id"] . "';");
$product_images = database::query("select * from " . DB_TABLE_PRODUCTS_IMAGES . " where product_id = '" . $row["id"] . "';");
while($product_image = database::fetch($product_images))
{
if(is_file(FS_DIR_HTTP_ROOT . WS_DIR_IMAGES . $product_image['filename']))
{
unlink(FS_DIR_HTTP_ROOT . WS_DIR_IMAGES . $product_image['filename']);
}
functions::image_delete_cache(FS_DIR_HTTP_ROOT . WS_DIR_IMAGES . $product_image['filename']);
database::query("delete from " . DB_TABLE_PRODUCTS_IMAGES . " where product_id = '" . $row["id"] . "' and id = '" . (int)$product_image['id'] . "' limit 1;");
}
}
}
}
}
exit();
}
但是上面發生的事情是刪除了數據庫中的所有記錄,在我的情況下,僅使用DELETE FROM x WHERE x NOT IN是不好的。
綜上所述,我有一個每天運行的程序,它將大約3000個條目發送到我的php腳本,然后將其添加/更新到數據庫中並從數據庫中刪除那些在最后3000條記錄中不存在的記錄。
有人知道我該怎么做嗎?
編輯:
我發現了罪魁禍首,似乎in_array($ row [“ ewe”],$ completed)不能正常工作,即使應該返回true,它也會返回false。
我手動檢查$ row [“ ewe”],它同時存在於數組和數據庫中,但in_array返回false。
編輯2:
找到了解決方案,似乎我的數組中有空格/換行符,我通過使用以下方法解決了該問題:$ completed = array_map(“ trim”,file(“ completed.txt”)); 現在它像魅力一樣工作了:)
查看數據庫關系和約束: https : //dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html
簡而言之,您可以定義沒有主記錄B(例如OBJECT)的記錄A(例如OBJECT_DETAIL)就不存在。 然后刪除母版的所有詳細信息也將被刪除。 這是您確保數據庫完整性的方式。
要在批次之間相交,合並或減去記錄,請為每個批次分配唯一的標識符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.