获取db的id,然后删除目录中的文件的最佳方法是什么


best way to take the id of db and then delete the file in the directory?

我正在尝试获取每个文件的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/>";
    }
?>

它输出:

  1. file1[deletelink]
  2. file2[deletelink]
  3. file3[deletelink]
  4. file4[deletelink]

print_r($result(

数组([0]=>数组([id_archivo]=>41[0]=>41[nombre_archivo]=>1294861647_-desktop.png[1]=>1294861847_-desk top.png[fecha]=>2011-08-08 15:10:09[2]=>2011-08-0815:10:09([1]=>阵列([id_archivo]=>42[0]=>42[nombre_archivo]=>Dragon_Ball_Simpsons.jpg[1]=>Dragon_Ball _Simpsons.jpg[fecha]=>2011-08-08 15:11:49[2]=>2011-08-0815:11:49([2]=>阵列([id_archivo]=>43[0]=>43[nombre_archivo]=>VATOS.jpg[1]=>VATOS.jpg[fecha]=>2011-08-08 16:30:00[2]=>2011-08.08 16:30:00()

好的,首先。。如果某个参数是一个数组,那么调用它$results将比调用$result更有逻辑性。此外,为什么echo "<input name='"" . $somearray['name'] . "'">";如果你可以简单地echo '<input name="' . $somearray['name'] . '">';

如果没有任何函数或特殊原因在这里添加缓冲区:$identifier = $array['name_file'];,那么你的问题可能是,你没有用unset();结束$identifier。所以我的第一个猜测是:

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/>';
    unset($identifier);
}

因为foreach生成了"$array",所以在加载新条目时将自动取消设置,但$identifier没有。这就是为什么您需要unset()来重置下一个条目的值。

然而,完全删除$identifier要容易得多:

foreach ($result as $array){
    echo  '<input type="hidden" id="id_file"   value="' . $array['id_file'].  ''">';
    echo $array['name_file'] . '<a id="' . urlencode($array['name_file']) . ''" href="#" class="delete" >Delete</a><br/>';
}

在您的代码中还有一些其他的要点,我会选择,但这应该基本上解决了问题。

编辑

根据你的评论,我创建了这些脚本它基本上是免费编程的:(正如您所理解的,您必须自己将自定义数据库类等添加到其中。但由于它们看起来是简单的数组。然后这应该做的技巧:

include '../Model/File.php';
$objFile = new File();
$files_array = $objFile->listFiles();

首先注意。。

我找不出任何原因$directory=dir($path(;

为什么这里有类型?你的脚本似乎无法处理上传。。$types=数组("jpg"、"jpeg"、"txt"、"gif"、"png"、"doc"、"docx","pdf"、"xlsx"、"pptx"(;

为什么在这里?如果您在提供的变量之上有其他变量代码,然后取消设置($identifier($identifier=";

由于我们在上面有一个示例数组,我们已经注释掉了这一部分$objFile=new File(($result=$objFile->listFiles((;

示例:http://kopli.pri.ee/stackoverflow/6986586.php

注意,我没有这些文件。。因此,示例是如何显示通用表以及jQuery效果。

主文件

<html>
<head>
    <title>Kalle rocks!</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
    <style>
        table {width: 600px; margin: 0px auto;}
        th, td {padding: 5px;}
        th {border-bottom: 1px solid black;}
        .tfoot {background: #EAEAEA;}
        .delete {background: #FFCECE; color: black; cursor: pointer; text-align: center;}
    </style>
</head>
<body>
<?php
// Where the files sit
$path = 'uploadedfiles/';
// Example array
$files_array[] = array(
    'id_archivo' => 41,
    'nombre_archivo' => '1294861647_-desktop.png',
    'fecha' => '2011-08-08 15:10:09'
);
$files_array[] = array(
    'id_archivo' => 42,
    'nombre_archivo' => 'Dragon_Ball_Simpsons.jpg',
    'fecha' => '2011-08-08 15:11:49 '
);
$files_array[] = array(
    'id_archivo' => 43,
    'nombre_archivo' => 'VATOS.jpg ',
    'fecha' => '2011-08-08 16:30:00'
);
// Start the table       
echo '<table cellpadding="0" cellspacing="0" border="0">
<thead>
<tr>
    <th>ID</th>
    <th>Name</th>
    <th>Date</th>
    <th>Delete</th>
</tr>
</thead>
<tbody>';
// Run the foreach loop
$files_displayed = 0;
foreach ($files_array as $fileid => $file){
    $files_displayed++;
    echo '<tr id="fileid_' . $fileid . '">
        <td>' . $fileid . '</td>
        <td>' . $file['nombre_archivo'] . '</td>
        <td>' . $file['fecha'] . '</td>
        <td class="delete"><span>[X]</span></td>
    </tr>';
}
// End the table and display totals
echo '</body>
<tfoot>
<tr>
    <td colspan="4" class="tfoot">Currently displaying ' . $files_displayed . ' files total</td>
</tr>
</tfoot>
</table>';
?>
<script>
$('.delete').click(function () {
    var file_row = $(this).parent('tr');
    var fileid = file_row.attr('id').replace('fileid_', '');
    $.ajax({
        type: 'get',
        url: 'ajax_file_delete.php',
        data: 'action=delete&fileid=' + fileid,
        beforeSend: function() {
        },
        success: function() {
            file_row.fadeOut('slow');
        }
    });
});
</script>
</body>
</html>

Ajax文件(Ajax_file_delete.php(

<?php
$path = 'uploadedfiles/';
// In theory, your include '../Model/File.php'; and $result= $objFile->listFiles(); should be included here, so the array would be imported
$files_array[] = array(
    'id_archivo' => 41,
    'nombre_archivo' => '1294861647_-desktop.png',
    'fecha' => '2011-08-08 15:10:09'
);
$files_array[] = array(
    'id_archivo' => 42,
    'nombre_archivo' => 'Dragon_Ball_Simpsons.jpg',
    'fecha' => '2011-08-08 15:11:49 '
);
$files_array[] = array(
    'id_archivo' => 43,
    'nombre_archivo' => 'VATOS.jpg ',
    'fecha' => '2011-08-08 16:30:00'
);
if ($_GET['delete']) {
    unlink($path . $files_array[$_GET['fileid']]['nombre_archivo']);
    // also, you can add here some mysql related stuff to delete the file from the db
}
?>

老实说,在这些事情上,我认为你应该至少使用一个$directory。可能在这里:$result= $objFile->listFiles($directory);

尝试var转储$results