如何将图片上传到亚马逊s3存储桶和子文件夹并使用php foreach显示

问题描述:

I am using the code here: http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/

...to create a bucket and subdirectories for users who upload pics for personal use when logged in.

After uploading the pics successfully to the s3 server, they are displayed in the html file in a list as such:

 <ul id="sortable-list">

    <?php
    $i = 0;
    $contents = $s3->getBucket("bucket_name");

    foreach($contents as $file)
    {

    $fname = $file['name'];                 
    $furl = "http://bucket_name.s3.amazonaws.com/".$fname;  

        echo "<li class=\"default\"><img src=\"$furl\" width=\"100\"></li>";

        {
            $i = 0;
        }

    }
    ?>
 </ul>

For some reason, the subfolder is getting recognized as a file inside the bucket and fills the first element of the $contents as http://bucket_name.s3.amazonaws.com/subfolder/, but of course is empty without referencing a filename.

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:

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

        $fileName = $_FILES['theFile']['name'];
        $fileTempName = $_FILES['theFile']['tmp_name'];
        $folderName = "subfolder/$fileName";

        $s3->putBucket("unique_id", S3::ACL_PUBLIC_READ);

        if ($s3->putObjectFile($fileTempName, "unique_id", $folderName, S3::ACL_PUBLIC_READ)) {
            echo "<strong>We successfully uploaded your file.</strong>";
        }else{
            echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
        }
    }

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

我在这里使用代码: http://net.tutsplus.com/tutorials/php /如何使用 - 亚马逊-s3-php-动态存储和管理文件 - 轻松/ p>

...创建一个 用户在登录时上传图片以供个人使用的用户的存储桶和子目录。 p>

将图片成功上传到s3服务器后,它们将在html文件中显示在列表中: / p>

 &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> 
 
 

出于某种原因,子文件夹 被识别为存储桶中的文件,并将 $ contents code>的第一个元素填充为 http://bucket_name.s3.amazonaws.com/subfolder/ code>,但是 当然是空的而没有引用文件名。 p>

我正在使用的php是上面引用的tutsplus.com教程中的page.php的修改形式。 有关创建存储桶和子目录的代码如下: p>

  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;我们已成功上传您的文件。&lt; / strong&gt;“; 
}其他{
 echo”&lt; strong&gt;上传文件时出错...抱歉。&lt; / strong&gt;“; \  n} 
} 
  code>  pre> 
 
 

需要帮助弄清楚如何防止子文件夹被识别为文件并在显示中创建元素。 非常感谢 p> div>

Several solutions:

  1. (Easiest) If it ends in a slash, it's a folder. i.e. check substr($fname,-1) == '/' and you have a folder

  2. (Neater) You can use the get_object_list call withe a PCRE to only get files that have something after the folder name. This works well if you know the folder name.

  3. (A possible, untested) Check $file['size'] - if zero then it's either a folder or an empty file. As you don't want empty files anyway, this could be an option.

after using putbucket() function use $folder name like this:

$s3->putBucket("file_bucket", S3::ACL_PUBLIC_READ);
            $foldername = "anirudh/$fileName";

and after that use these function

if ($s3->putObjectFile($fileTempName, "file_bucket",
           $foldername, S3::ACL_PUBLIC_READ))