1

I got a problem with the following php code. It is supposed to list the items of a S3 bucket and find&delete files which contain a certain string in their filenames. Problem is: only one file is deleted the others remain on the bucket after the execution of the script. I can't find where the issue comes from so I ask you :/

$aS3Files = $s3->getBucket($bucketName); // list all elements in the bucket
$query = mysql_query("SELECT filename FROM prizes_media WHERE prize_id=" . $_POST["prizeId"]); // finds all filenames linked to the prize
while($media = mysql_fetch_array($query)){  
    // Find relevant files
    while ( list($cFilename, $rsFileData) = each($aS3Files) ) { // reformat the bucket list into a table and reads through it
        if(strpos($cFilename,$media['filename'])) {
            $s3->deleteObject($bucketName, $cFilename); // deletes all files that contain $media['filename'] in their filename
        }
    }
}

// 2. Delete DB entry
mysql_query("DELETE FROM prizes WHERE id=" . $_POST['prizeId'] ); // deletes the entry correponding to the prize in the DB (deletes media table in cascade)
1
  • Alright, the solution was to either: 1. add reset($aS3Files) cf. stackoverflow.com/questions/3304885/… 2. replace "while list each" by foreach( $aS3Files as $cFilename => $rsFileData ) Commented Mar 11, 2012 at 3:19

1 Answer 1

0

You may be getting false negatives on your if, you should be using this:

if(strpos($cFilename,$media['filename']) !== FALSE) { ...

Edit

Here is a different way to loop the bucket, based on the structure on your comment:

foreach($aS3Files as $filename => $filedata) {
    if(strpos($filename, $media['filename']) !== FALSE) {
       $s3->deleteObject($bucketName, $filename); // deletes all files that contain $media['filename'] in their filename
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Just tried it but I still have the same problem. I'm gonna try listing the bucket content inside the while loop even if it is heavy on resources...
It works when $aS3Files = $s3->getBucket($bucketName); is put inside the first loop. I'd prefer not having to do that as the bucket is small at the moment but that won't be the case in a few months :s
Something must be breaking one of the two loops early
Do you know another way of parsing $aS3Files, different from list($cFilename, $rsFileData) = each($aS3Files)?
$aS3Files format is as follow: Array ( [birdviews/0367b859cdb55b33d24e6849cd1d7b67.jpg] => Array ( [name] => birdviews/0367b859cdb55b33d24e6849cd1d7b67.jpg [time] => 1331220510 [size] => 177630 [hash] => 0367b859cdb55b33d24e6849cd1d7b67 ) [birdviews/0e11f6467e3a8c10334a322d03b17e7c.jpg] => Array ( [name] => birdviews/0e11f6467e3a8c10334a322d03b17e7c.jpg [time] => 1331220512 [size] => 133705 [hash] => 0e11f6467e3a8c10334a322d03b17e7c )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.