PHP,从TXT文件中删除路径

发布于 2024-10-24 15:55:46 字数 923 浏览 2 评论 0原文

我有问题..我用按钮“删除”显示 ARRAY 中目录中的图像 - 操作delete.php..

如果我单击“删除”文件delete.php 应该从目录中删除图像并从 TXT 文件中删除路径。 下面的 PHP 代码仅从 dir 中删除文件,我不知道如何从 TXT 文件中删除 PATH - 我需要这个脚本..

TXT 文件看起来:

../../gallery/glowna//thumb_1300625269.jpg|
../../gallery/glowna//thumb_1300625300.jpg|
../../gallery/glowna/thumb_1300626725.jpg

和 delete.php

<?php

$plik=$_POST['usun'];
$nowa = substr($plik, 6, 20);

unlink('../../gallery/glowna/'.$_POST['usun']);
unlink('../../gallery/glowna/'.$nowa);

header("location:usun.php");

?>

我尝试使用下面的代码,但出了点问题,因为TXT 文件正在清理全部:

$txt = "../../dynamic_ajax.txt";
$img = "../../gallery/glowna/".$_POST['usun'];

$file = file_get_contents($txt, true);
$file2 = explode('|', $file);
$search=array_search($img, $file2);

unset($search);

$separator = implode("|", $file2);

file_put_contents($txt, $separator);

I have, problem.. I display images from dir in ARRAY with button 'delete' - action delete.php..

If I click 'delete' file delete.php should delete image from dir and path from TXT file..
Below PHP code delete only file from dir, I don't know how I can delete PATH from TXT files - I need this script..

TXT file looking that:

../../gallery/glowna//thumb_1300625269.jpg|
../../gallery/glowna//thumb_1300625300.jpg|
../../gallery/glowna/thumb_1300626725.jpg

And delete.php

<?php

$plik=$_POST['usun'];
$nowa = substr($plik, 6, 20);

unlink('../../gallery/glowna/'.$_POST['usun']);
unlink('../../gallery/glowna/'.$nowa);

header("location:usun.php");

?>

I trying use below code, but something is wrong, because TXT file are cleaning ALL:

$txt = "../../dynamic_ajax.txt";
$img = "../../gallery/glowna/".$_POST['usun'];

$file = file_get_contents($txt, true);
$file2 = explode('|', $file);
$search=array_search($img, $file2);

unset($search);

$separator = implode("|", $file2);

file_put_contents($txt, $separator);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-10-31 15:55:46

好吧,我明白你的意思了。这是我记下的内容,您可能需要稍微清理一下代码。

$q      = 'thumb_1300625300.jpg';
$files  = file_get_contents('files.txt');
$arr    = explode('|', $files);
foreach ($arr as &$file) {
    if (strpos($file, $q) !== false) {
        $file = '';
        break; 
    } 
}
$files  = implode('|', $arr);
$files  = str_ireplace('||', '|', $files);
file_put_contents('files.txt', $files);

非常简单的代码。

  1. 打开文件并按 | 分割它
  2. 然后它循环遍历数组,寻找与图像匹配的路径并将其清空,然后跳过循环
  3. 然后您将字符串内爆,然后删除双 |因为我们删除了一个元素

一些警告。该脚本仅查找路径的一个实例。如果有多个,则让循环运行并删除 break。您还需要修改 str_ireplace('||', '|', $files); 以便它查找多个 |

Ok think I understand what you mean. This is something I jotted down, you might want to clean up the code a bit.

$q      = 'thumb_1300625300.jpg';
$files  = file_get_contents('files.txt');
$arr    = explode('|', $files);
foreach ($arr as &$file) {
    if (strpos($file, $q) !== false) {
        $file = '';
        break; 
    } 
}
$files  = implode('|', $arr);
$files  = str_ireplace('||', '|', $files);
file_put_contents('files.txt', $files);

Pretty simple code.

  1. Opens up the file and splits it by |
  2. Then it loops through the arrray looking for the path that matches the image and makes it empty and then skips the loop
  3. Then you implode the string and then remove the double | because we removed an element

A couple of caveats. This script only looks for one instance of the path. If you have multiple, then let the loop run its course and remove the break. You also need to modify str_ireplace('||', '|', $files); so that it will look for multiple |

遇见了你 2024-10-31 15:55:46

这又如何呢?

$file = file_get_contents($txt, true);
$file2 = explode('|', $file);

$new_array = Array();

foreach ($file2 as $path) {
  if (/* path should be preserved */) {
    $new_array[] = $path;
  }
}

$new_contents = implode("|", $new_array);
file_put_contents($txt, $new_contents);

但请注意,将其放在公共服务器上一段时间后,您的 TXT 文件就会消失。想象一下:

  • 第一个进程(线程)打开文件进行写入(将其截断为 0 个字符)。
  • 第二个进程读取空文件。
  • 第 1 页。写好文件。
  • 第二个进程写入空文件。

您可以通过使用某种锁定机制来解决这个问题,但请考虑其他选项。如果该文件中只有路径,为什么不为图像建立一个特殊的文件夹呢?然后只需列出该文件夹,您就知道存在哪些文件。如果您想随图像一起保存一些元数据,数据库就是您的朋友。

What about this?

$file = file_get_contents($txt, true);
$file2 = explode('|', $file);

$new_array = Array();

foreach ($file2 as $path) {
  if (/* path should be preserved */) {
    $new_array[] = $path;
  }
}

$new_contents = implode("|", $new_array);
file_put_contents($txt, $new_contents);

But be aware that a little while after you put this on a public server, your TXT file will be gone. Imagine this:

  • 1st process (thread) opens the file for writing (truncating it to 0 characters).
  • 2nd process reads the empty file.
  • 1st p. writes good file.
  • 2nd process writes empty file.

You could get around that by using some lock mechanism, but consider other options. If you have only paths in that file, why not having a special folder for your images? Then just list that folder and you know which files are present. If you want to save some metadata with the images, database is your friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文