获取db的id然后删除目录中的文件的最佳方法?

发布于 2024-11-28 06:41:27 字数 1517 浏览 2 评论 0 原文

我试图获取每个文件的 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($结果)

数组 ( [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 ) )

I am trying to get the id of each file, then delete them all with a hidden input, I dont know If I am doing wrong, are there better ways what would be your approach??. My code is for deleting a file first the id of the db and then the file of the folder

This is my code:

<?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/>";
    }

?>

it outputs:

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

print_r($result)

Array ( [0] => Array ( [id_archivo] => 41 [0] => 41 [nombre_archivo]
=> 1294861647_-desktop.png [1] => 1294861647_-desktop.png [fecha] =>
2011-08-08 15:10:09 [2] => 2011-08-08 15:10:09 ) [1] => Array (
[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-08 15:11:49 ) [2] => Array (
[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 )
)

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

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

发布评论

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

评论(2

七秒鱼° 2024-12-05 06:41:28

以我诚实的观点,在这些事情上,我认为你应该至少使用 $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.

无所谓啦 2024-12-05 06:41:27

首先,如果某个参数是一个数组,那么调用它 $results 会比 $result 更逻辑。另外,为什么要 echo ""; 如果您可以简单地 echo '';

如果没有任何功能或特殊原因需要在此处添加缓冲区: $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 = array('jpg', 'jpeg', 'txt', 'gif', 'png', 'doc', 'docx',
'pdf', 'xlsx', 'pptx');

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

由于我们上面有一个示例数组,因此我们已将这部分注释掉
$objFile = 新文件(); $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
}

?>

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 simply echo '<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 with unset();. So my first guess would be this:

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);
}

Because foreach generated "$array", will be unset automatically when new entry gets loaded, but $identifier doesn't. That's why you need unset() to reset the value for the next entry.

However much more easier would be to remove the $identifier altogether:

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/>';
}

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:

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

First some notes..

I cant find any reason for this.. $directory = dir($path);

Why are the types here? Your script doesnt seem to deal with uploads..
$types = array('jpg', 'jpeg', 'txt', 'gif', 'png', 'doc', 'docx',
'pdf', 'xlsx', 'pptx');

Why is this here? If you have some other variable above your provided
code, then unset($identifier); $identifier = "";

Since we have an example array above, we have commented this part out
$objFile = new File(); $result = $objFile->listFiles();

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

<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 file (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
}

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