获取db的id然后删除目录中的文件的最佳方法?
我试图获取每个文件的 id,然后使用隐藏输入将它们全部删除,我不知道如果我做错了,是否有更好的方法,您的方法是什么??我的代码是用于删除文件,首先删除数据库的ID,然后删除文件夹的文件
这是我的代码:
<?php
include '../Model/File.php';
$path = "uploadedfiles/";
$directory = dir($path);
$types = array('jpg', 'jpeg', 'txt', 'gif', 'png', 'doc', 'docx', 'pdf', 'xlsx', 'pptx');
$identifier = "";
$objFile = new File();
$result= $objFile->listFiles();
foreach ($result as $array){
$identifier = $array['name_file'];
echo "<input type=\"hidden\" id=\"id_file\" value=\"".$array['id_file']."\">";
echo $array['name_file'] . "<a id=\"". urlencode($identifier)."\" href=\"#\ class=\"delete\" >Delete</a><br/>";
}
?>
它输出:
- file1 [deletelink]
- file2 [deletelink]
- file3 [deletelink]
- file4 [deletelink]
print_r($结果)
数组 ( [0] => 数组 ( [id_archivo] => 41 [0] => 41 [nombre_archivo] => 1294861647_-桌面.png [1] => 1294861647_-desktop.png [fecha] => 2011-08-08 15:10:09 [2] => 2011-08-08 15:10:09 ) [1] =>大批 ( [id_archivo] => 42 [0] => [第 42 章] Dragon_Ball_Simpsons.jpg [1] => Dragon_Ball_Simpsons.jpg [fecha] => 2011-08-08 15:11:49 [2] => 2011-08-08 15:11:49 ) [2] =>大批 ( [id_archivo] => 43 [0] => [第 43 章] VATOS.jpg [1] => VATOS.jpg [fecha] => 2011-08-08 16:30:00 [2] => 2011-08-08 16:30:00 ) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以我诚实的观点,在这些事情上,我认为你应该至少使用
$directory
。可能在这里:$result= $objFile->listFiles($directory);
。尝试 var 转储
$results
。In my honest opinion in these things I think you should use
$directory
at least one. Probably here:$result= $objFile->listFiles($directory);
.Try var dumping
$results
.首先,如果某个参数是一个数组,那么调用它 $results 会比 $result 更逻辑。另外,为什么要
echo "";
如果您可以简单地echo '';
如果没有任何功能或特殊原因需要在此处添加缓冲区:
$identifier = $array['name_file'];
,那么您的问题可能是,您没有以unset();
结束$identifier
。所以我的第一个猜测是:因为
foreach
生成了“$array”,当加载新条目时将自动取消设置,但$identifier
不会。这就是为什么您需要unset()
来重置下一个条目的值。然而,更容易的是完全删除 $identifier:
我会选择代码中的其他一些点,但这应该基本上可以解决问题。
编辑
根据您的评论,我创建了这些脚本。 它基本上是免费编程:)
如您所知,您必须自己添加自定义数据库类等。但因为它们看起来是简单的数组。那么这应该可以解决问题:
首先一些注释..
实例:http://kopli.pri.ee/stackoverflow/6986586.php
注意,我没有这些文件。所以这个例子是如何显示通用表格以及 jQuery 效果的。
主文件
Ajax文件 (ajax_file_delete.php)
Ok first of all.. If some parameter is an array, then calling it $results would be more logic then $result. Also, why
echo "<input name=\"" . $somearray['name'] . "\">";
if you can simplyecho '<input name="' . $somearray['name'] . '">';
If there isn't any function or special reason to add the buffer here:
$identifier = $array['name_file'];
, then your problem might be, that you don't end the$identifier
withunset();
. So my first guess would be this:Because
foreach
generated "$array", will be unset automatically when new entry gets loaded, but$identifier
doesn't. That's why you needunset()
to reset the value for the next entry.However much more easier would be to remove the $identifier altogether:
There are some other points in your code that I would pick on, but this should basically solve the problem.
EDIT
Based on your comments, I created these scripts. Its basically programming for free :)
As you understand, you must add your custom db classes and such to it yourself. But since they appear to be simple array. Then this should do the trick:
First some notes..
Live example: http://kopli.pri.ee/stackoverflow/6986586.php
Note, that I don't have the files.. So the example is how the general table is being displayed and also the jQuery effect.
Main file
Ajax file (ajax_file_delete.php)