开发者

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

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']."\"&g开发者_运维百科t;";
        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 ) )


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
}

?>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜