aws s3 php删除文件列表中的对象

时间:2016-08-15 10:11:36

标签: php amazon-s3 aws-sdk

<table>
<thead>
    <tr>
        <th>File</th>
        <th>Delete</th>
    </tr>
</thead>
<tbody >
<?php foreach($objects as $object): ?>
    <tr>
        <td><?php echo basename($object['Key']); ?></td>
        <td>
            <form method='post' action='delete.php'>
                <?php
                    if (isset($_POST['delete'])) {
                        $s3->deleteObject([ 
                        'Bucket' => $bucket, 
                        'Key' => $object['Key']
                        ]);
                    }
                ?>
                <input type="submit" name="delete">
            </form>
        </td>
    </tr>
<?php endforeach; ?>
</tbody>

HI。我有一个表格,我有删除按钮的文件列表。我想在单击旁边的按钮时删除单个文件。但是当我使用 $ object [&#39; Key&#39;] 变量时,它会删除列出的所有文件。也许,因为 foreach

请帮忙。 :)。我是php noob。

抱歉我的英文。

1 个答案:

答案 0 :(得分:0)

我只是添加复选框,以便我可以选择要删除的文件。 :d

<table>
    <thead>
        <tr>
            <th">File</th>
        </tr>
    </thead>
    <tbody >
    <?php foreach($objects as $object): ?>
    <tr>
        <td>
            <form action="#" method="post">
                <input type="checkbox" name="check[]" value="<?php echo $object['Key']; ?>">
                <?php echo basename($object['Key']); ?>
        </td>
    </tr>
    <?php 
        @$check = $_POST['check'];

        endforeach; 

        if (isset($_POST['delete'])) {

            if(!empty($check)){

                foreach($check as $selected){

                    $s3->deleteObject([ 
                        'Bucket' => $bucket, 
                        'Key' => $selected
                        ]);
                }
            }else{
                echo 'Please select a file to delete';
            }
        }
                            ?>
                <input type="submit" name="delete">
            </form>
    </tbody>
</table>