title: How to display S3 files for each user using PHP foreach
body: <p>I am using Amazon S3 to store photos uploaded by users, using the following code:</p>
  
  <pre><code>        if(isset($_POST['Submit'])){
  
          $fileName = $_FILES['theFile']['name'];
          $fileTempName = $_FILES['theFile']['tmp_name'];
          $folderName = "{id}/{address}/$fileName";
  
          //create a new bucket
          $s3-&gt;putBucket("bucket_name", S3::ACL_PUBLIC_READ);
  
          //move the file
          if ($s3-&gt;putObjectFile($fileTempName, "bucket_name", $folderName, S3::ACL_PUBLIC_READ)) {
              echo ""; //etc, etc
  </code></pre>
  
  <p>The <code>{id}</code> and <code>{address}</code> are dynamic variables used to create the appearance of folders when the files are uploaded, based on each user's unique data stored in a mysql database.</p>
  
  <p>The problem I'm having is displaying these files inside each user's password protected site. The code I'm using below pics up ALL user's photos and displays them to ALL users. If <code>{id}/{address}/$fileName</code> converts to <code>123/figuero/photo.jpg</code> then user <code>123</code> should only be able to see <code>123/figuero/photo.jpg</code> (or any other files with the preface <code>123/</code> or <code>123/xxx/</code></p>
  
  <p>With the code I'm using below, ALL files inside <code>bucket_name</code> can be seen by ALL users, instead of just the files associated with the dynamic data associated with their photos:</p>
  
  <pre><code>    &lt;ul&gt;
          &lt;?php
              $i = 0;
              $contents = $s3-&gt;getBucket("bucket_name");
  
               foreach($contents as $file)
               {
  
               $fname = $file['name'];
               $furl = 'http://bucket_name.s3.amazonaws.com/'.$fname;
  
                   echo "&lt;li&gt;&lt;img src='$furl'&gt;&lt;/li&gt;";
                   {
                       $i = 0; 
                   }
               }
          ?&gt;
      &lt;ul&gt;
  </code></pre>
  
  <p>In other words, <em>ALL</em> the files inside <a href="http://bucket_name.S3.amazonaws.com/" rel="nofollow">http://bucket_name.S3.amazonaws.com/</a> are being pulled up for display by the code, instead of just the photos pertaining to user <code>123</code> inside the files prefaced by <code>123/xxx/</code></p>
  
  <p>What am I missing? Thanks much in advance.</p>