title: How to upload pic to Amazon s3 bucket and subfolder and display using php foreach
body: <p>I am using the code here: <a href="http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/" rel="nofollow">http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/</a></p>
  
  <p>...to create a bucket and subdirectories for users who upload pics for personal use when logged in. </p>
  
  <p>After uploading the pics successfully to the s3 server, they are displayed in the html file in a list as such:</p>
  
  <pre><code> &lt;ul id="sortable-list"&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 class=\"default\"&gt;&lt;img src=\"$furl\" width=\"100\"&gt;&lt;/li&gt;";
  
          {
              $i = 0;
          }
  
      }
      ?&gt;
   &lt;/ul&gt;
  </code></pre>
  
  <p>For some reason, the subfolder is getting recognized as a file inside the bucket and fills the first element of the <code>$contents</code> as <code>http://bucket_name.s3.amazonaws.com/subfolder/</code>, but of course is empty without referencing a filename.</p>
  
  <p>The php I'm using is a modified form of page.php on the tutsplus.com tutorial referenced above. The code that pertains to the creation of the bucket and sub-directories is here:</p>
  
  <pre><code>    if(isset($_POST['Submit'])){
  
          $fileName = $_FILES['theFile']['name'];
          $fileTempName = $_FILES['theFile']['tmp_name'];
          $folderName = "subfolder/$fileName";
  
          $s3-&gt;putBucket("unique_id", S3::ACL_PUBLIC_READ);
  
          if ($s3-&gt;putObjectFile($fileTempName, "unique_id", $folderName, S3::ACL_PUBLIC_READ)) {
              echo "&lt;strong&gt;We successfully uploaded your file.&lt;/strong&gt;";
          }else{
              echo "&lt;strong&gt;Something went wrong while uploading your file... sorry.&lt;/strong&gt;";
          }
      }
  </code></pre>
  
  <p>Need help figuring out how to prevent the subfolder from being recognized as a file and creating an element in in the display. Thanks much</p>