PHP,从TXT文件中删除路径
我有问题..我用按钮“删除”显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我明白你的意思了。这是我记下的内容,您可能需要稍微清理一下代码。
非常简单的代码。
一些警告。该脚本仅查找路径的一个实例。如果有多个,则让循环运行并删除
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.
Pretty simple code.
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 modifystr_ireplace('||', '|', $files);
so that it will look for multiple |这又如何呢?
但请注意,将其放在公共服务器上一段时间后,您的 TXT 文件就会消失。想象一下:
您可以通过使用某种锁定机制来解决这个问题,但请考虑其他选项。如果该文件中只有路径,为什么不为图像建立一个特殊的文件夹呢?然后只需列出该文件夹,您就知道存在哪些文件。如果您想随图像一起保存一些元数据,数据库就是您的朋友。
What about this?
But be aware that a little while after you put this on a public server, your TXT file will be gone. Imagine this:
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.