duanliaolan6178 2012-11-20 00:12
浏览 121
已采纳

如何将图片上传到亚马逊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

  • 写回答

2条回答 默认 最新

  • douruduan8812 2012-11-20 01:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教